@@ -4,54 +4,63 @@ import { z } from 'zod';
44import { config } from '../../config' ;
55
66const generateApiKeySchema = z . object ( {
7- name : z . string ( ) . min ( 1 , 'API kay name must be more than 1 characters' ) . max ( 255 , 'API kay name must not be more than 255 characters' ) ,
8- expiresInDays : z . number ( ) . int ( ) . positive ( 'Only positive number is allowed' ) . max ( 730 , "The API key must expire within 2 years / 730 days." ) ,
7+ name : z
8+ . string ( )
9+ . min ( 1 , 'API kay name must be more than 1 characters' )
10+ . max ( 255 , 'API kay name must not be more than 255 characters' ) ,
11+ expiresInDays : z
12+ . number ( )
13+ . int ( )
14+ . positive ( 'Only positive number is allowed' )
15+ . max ( 730 , 'The API key must expire within 2 years / 730 days.' ) ,
916} ) ;
1017
1118export class ApiKeyController {
12- public async generateApiKey ( req : Request , res : Response ) {
13- if ( config . app . isDemo ) {
14- return res . status ( 403 ) . json ( { message : req . t ( 'errors.demoMode' ) } ) ;
15- }
16- try {
17- const { name, expiresInDays } = generateApiKeySchema . parse ( req . body ) ;
18- if ( ! req . user || ! req . user . sub ) {
19- return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
20- }
21- const userId = req . user . sub ;
22-
23- const key = await ApiKeyService . generate ( userId , name , expiresInDays ) ;
24-
25- res . status ( 201 ) . json ( { key } ) ;
26- } catch ( error ) {
27- if ( error instanceof z . ZodError ) {
28- return res . status ( 400 ) . json ( { message : req . t ( 'api.requestBodyInvalid' ) , errors : error . message } ) ;
29- }
30- res . status ( 500 ) . json ( { message : req . t ( 'errors.internalServerError' ) } ) ;
31- }
32- }
33-
34- public async getApiKeys ( req : Request , res : Response ) {
35- if ( ! req . user || ! req . user . sub ) {
36- return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
37- }
38- const userId = req . user . sub ;
39- const keys = await ApiKeyService . getKeys ( userId ) ;
40-
41- res . status ( 200 ) . json ( keys ) ;
42- }
43-
44- public async deleteApiKey ( req : Request , res : Response ) {
45- if ( config . app . isDemo ) {
46- return res . status ( 403 ) . json ( { message : req . t ( 'errors.demoMode' ) } ) ;
47- }
48- const { id } = req . params ;
49- if ( ! req . user || ! req . user . sub ) {
50- return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
51- }
52- const userId = req . user . sub ;
53- await ApiKeyService . deleteKey ( id , userId ) ;
54-
55- res . status ( 204 ) . send ( { message : req . t ( 'apiKeys.deleteSuccess' ) } ) ;
56- }
19+ public async generateApiKey ( req : Request , res : Response ) {
20+ if ( config . app . isDemo ) {
21+ return res . status ( 403 ) . json ( { message : req . t ( 'errors.demoMode' ) } ) ;
22+ }
23+ try {
24+ const { name, expiresInDays } = generateApiKeySchema . parse ( req . body ) ;
25+ if ( ! req . user || ! req . user . sub ) {
26+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
27+ }
28+ const userId = req . user . sub ;
29+
30+ const key = await ApiKeyService . generate ( userId , name , expiresInDays ) ;
31+
32+ res . status ( 201 ) . json ( { key } ) ;
33+ } catch ( error ) {
34+ if ( error instanceof z . ZodError ) {
35+ return res
36+ . status ( 400 )
37+ . json ( { message : req . t ( 'api.requestBodyInvalid' ) , errors : error . message } ) ;
38+ }
39+ res . status ( 500 ) . json ( { message : req . t ( 'errors.internalServerError' ) } ) ;
40+ }
41+ }
42+
43+ public async getApiKeys ( req : Request , res : Response ) {
44+ if ( ! req . user || ! req . user . sub ) {
45+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
46+ }
47+ const userId = req . user . sub ;
48+ const keys = await ApiKeyService . getKeys ( userId ) ;
49+
50+ res . status ( 200 ) . json ( keys ) ;
51+ }
52+
53+ public async deleteApiKey ( req : Request , res : Response ) {
54+ if ( config . app . isDemo ) {
55+ return res . status ( 403 ) . json ( { message : req . t ( 'errors.demoMode' ) } ) ;
56+ }
57+ const { id } = req . params ;
58+ if ( ! req . user || ! req . user . sub ) {
59+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
60+ }
61+ const userId = req . user . sub ;
62+ await ApiKeyService . deleteKey ( id , userId ) ;
63+
64+ res . status ( 204 ) . send ( { message : req . t ( 'apiKeys.deleteSuccess' ) } ) ;
65+ }
5766}
0 commit comments