-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathindex.ts
72 lines (66 loc) · 2.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { Response } from 'express';
import Controller from '../controller';
import type { IUnleashConfig, IUnleashServices } from '../../types';
import type { Logger } from '../../logger';
import { NONE } from '../../types/permissions';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
import type { RequestBody } from '../unleash-types';
import { createRequestSchema } from '../../openapi/util/create-request-schema';
import {
validatedEdgeTokensSchema,
type ValidatedEdgeTokensSchema,
} from '../../openapi/spec/validated-edge-tokens-schema';
import type EdgeService from '../../services/edge-service';
import type { OpenApiService } from '../../services/openapi-service';
import { getStandardResponses } from '../../openapi/util/standard-responses';
import type { TokenStringListSchema } from '../../openapi';
export default class EdgeController extends Controller {
private readonly logger: Logger;
private edgeService: EdgeService;
private openApiService: OpenApiService;
constructor(
config: IUnleashConfig,
{
edgeService,
openApiService,
}: Pick<IUnleashServices, 'edgeService' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('edge-api/index.ts');
this.edgeService = edgeService;
this.openApiService = openApiService;
this.route({
method: 'post',
path: '/validate',
handler: this.getValidTokens,
permission: NONE,
middleware: [
this.openApiService.validPath({
tags: ['Edge'],
security: [{}],
summary: 'Check which tokens are valid',
description:
'This operation accepts a list of tokens to validate. Unleash will validate each token you provide. For each valid token you provide, Unleash will return the token along with its type and which projects it has access to.',
operationId: 'getValidTokens',
requestBody: createRequestSchema('tokenStringListSchema'),
responses: {
200: createResponseSchema('validatedEdgeTokensSchema'),
...getStandardResponses(400, 413, 415),
},
}),
],
});
}
async getValidTokens(
req: RequestBody<TokenStringListSchema>,
res: Response<ValidatedEdgeTokensSchema>,
): Promise<void> {
const tokens = await this.edgeService.getValidTokens(req.body.tokens);
this.openApiService.respondWithValidation<ValidatedEdgeTokensSchema>(
200,
res,
validatedEdgeTokensSchema.$id,
tokens,
);
}
}