Skip to content

Commit f28ddfd

Browse files
committed
chore: add-security-to-swagger
1 parent cf73f8b commit f28ddfd

4 files changed

Lines changed: 19 additions & 10 deletions

File tree

e2e/mock-api-v2/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"description": "",
66
"type": "module",
7-
"main": "./dist/index.js",
7+
"main": "./src/main.js",
88
"scripts": {
99
"build": "pnpm nx nxBuild",
1010
"lint": "pnpm nx nxLint",
@@ -28,7 +28,7 @@
2828
"executor": "@nx/js:tsc",
2929
"outputs": ["{options.outputPath}"],
3030
"options": {
31-
"outputPath": "./dist",
31+
"outputPath": "e2e/mock-api-v2/dist",
3232
"main": "e2e/mock-api-v2/src/main.ts",
3333
"tsConfig": "e2e/mock-api-v2/tsconfig.app.json",
3434
"generatePackageJson": true

e2e/mock-api-v2/src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ const ServerMock = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
3636
Layer.provide(UserInfoMockService),
3737
Layer.provide(IncrementStepIndexMock),
3838
Layer.provide(AuthorizationMock),
39-
Layer.provide(HttpApiBuilder.middlewareCors()),
39+
Layer.provide(
40+
HttpApiBuilder.middlewareCors({
41+
allowedMethods: ['GET', 'PUT', 'POST', 'OPTIONS'],
42+
allowedOrigins: ['http://localhost:5173', 'http://localhost:8443'],
43+
credentials: true,
44+
maxAge: 3600,
45+
}),
46+
),
4047
HttpServer.withLogAddress,
4148
Layer.provide(NodeHttpServer.layer(createServer, { port: 9443 })),
4249
);

e2e/mock-api-v2/src/middleware/Authorization.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Unauthorized } from '@effect/platform/HttpApiError';
2-
import { HttpApiMiddleware, HttpApiSecurity } from '@effect/platform';
2+
import { HttpApiMiddleware, HttpApiSecurity, OpenApi } from '@effect/platform';
33
import { Brand, Context, Effect, Layer, Redacted } from 'effect';
44

55
type BearerTokenValue = string & Brand.Brand<'BearerToken'>;
@@ -10,12 +10,13 @@ class BearerToken extends Context.Tag('BearerToken')<BearerToken, BearerTokenVal
1010

1111
class Authorization extends HttpApiMiddleware.Tag<Authorization>()('Authorization', {
1212
failure: Unauthorized,
13-
provides: BearerToken, // Declare that this middleware provides the bearer token
13+
provides: BearerToken,
1414
security: {
15-
myBearer: HttpApiSecurity.bearer,
15+
myBearer: HttpApiSecurity.bearer.pipe(
16+
HttpApiSecurity.annotate(OpenApi.Description, 'Bearer token for API authentication'),
17+
),
1618
},
1719
}) {}
18-
1920
const AuthorizationMock = Layer.effect(
2021
Authorization,
2122
Effect.gen(function* () {

e2e/mock-api-v2/src/spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const MockApi = HttpApi.make('MyApi')
4747
.add(
4848
HttpApiGroup.make('Tokens')
4949
.add(
50-
HttpApiEndpoint.post('Tokens')`/envid/as/token`
50+
HttpApiEndpoint.post('Tokens')`/:envid/as/token`
5151
.addSuccess(TokenResponseBody)
5252
.addError(HttpApiError.Unauthorized)
5353
.setPath(Schema.Struct({ envid: Schema.String })),
@@ -62,8 +62,9 @@ const MockApi = HttpApi.make('MyApi')
6262
.addSuccess(UserInfoSchema)
6363
.addError(HttpApiError.Unauthorized),
6464
)
65+
.middleware(Authorization)
6566
.annotate(OpenApi.Description, 'User Info route that requires a bearer token')
6667
.annotate(OpenApi.Description, 'Protected routes, that require a bearer token'),
67-
)
68-
.middleware(Authorization);
68+
);
69+
6970
export { MockApi };

0 commit comments

Comments
 (0)