Skip to content

Commit a443b9a

Browse files
committed
Fix swagger build
1 parent 5a1a73c commit a443b9a

File tree

6 files changed

+40
-25
lines changed

6 files changed

+40
-25
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ clean:
5656
rm -rf dist_devel/
5757
rm -rf coverage/
5858

59+
build_swagger:
60+
cd src/api && npx tsx --experimental-loader=./mockLoader.mjs createSwagger.ts && cd ../..
61+
5962
build: src/ cloudformation/
6063
yarn -D
6164
VITE_BUILD_HASH=$(GIT_HASH) yarn build
62-
cd src/api && npx tsx --experimental-loader=./mockLoader.mjs createSwagger.ts
65+
make build_swagger
6366
cp -r src/api/resources/ dist/api/resources
6467
rm -rf dist/lambda/sqs
6568
sam build --template-file cloudformation/main.yml --use-container --parallel

src/api/createSwagger.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { fileURLToPath } from "url";
22
import path from "node:path";
3-
import { writeFile, mkdir } from "fs/promises";
3+
import { writeFile, mkdir, rm } from "fs/promises";
44
import init from "./index.js"; // Assuming this is your Fastify app initializer
5-
import { docsHtml } from "./docs.js";
5+
import { docsHtml, securitySchemes } from "./docs.js";
6+
import yaml from "yaml";
7+
68
/**
79
* Generates and saves Swagger/OpenAPI specification files.
810
*/
@@ -14,9 +16,16 @@ async function createSwaggerFiles() {
1416
const __filename = fileURLToPath(import.meta.url);
1517
const __dirname = path.dirname(__filename);
1618
const outputDir = path.resolve(__dirname, "..", "..", "dist_ui", "docs");
19+
await rm(outputDir, { recursive: true, force: true });
1720
await mkdir(outputDir, { recursive: true });
18-
const jsonSpec = JSON.stringify(app.swagger(), null, 2);
19-
const yamlSpec = app.swagger({ yaml: true });
21+
const swaggerConfig = app.swagger();
22+
const realSwaggerConfig = JSON.parse(JSON.stringify(swaggerConfig));
23+
realSwaggerConfig.components = realSwaggerConfig.components || {};
24+
realSwaggerConfig.components.securitySchemes = securitySchemes;
25+
const jsonSpec = JSON.stringify(realSwaggerConfig, null, 2);
26+
const doc = new yaml.Document();
27+
doc.contents = realSwaggerConfig;
28+
const yamlSpec = doc.toString();
2029
await writeFile(path.join(outputDir, "openapi.json"), jsonSpec);
2130
await writeFile(path.join(outputDir, "openapi.yaml"), yamlSpec);
2231
await writeFile(path.join(outputDir, "index.html"), docsHtml);

src/api/docs.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,18 @@ export const docsHtml = `
136136
</body>
137137
</html>
138138
`;
139+
140+
export const securitySchemes = {
141+
bearerAuth: {
142+
type: "http",
143+
scheme: "bearer",
144+
bearerFormat: "JWT",
145+
description:
146+
"Authorization: Bearer {token}\n\nThis API uses JWT tokens issued by Entra ID (Azure AD) with the Core API audience. Tokens must be included in the Authorization header as a Bearer token for all protected endpoints.",
147+
},
148+
apiKeyAuth: {
149+
type: "apiKey",
150+
in: "header",
151+
name: "X-Api-Key",
152+
},
153+
};

src/api/index.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import protectedRoute from "./routes/protected.js";
6161
import eventsPlugin from "./routes/events.js";
6262
import mobileWalletV2Route from "./routes/v2/mobileWallet.js";
6363
import membershipV2Plugin from "./routes/v2/membership.js";
64-
import { docsHtml } from "./docs.js";
64+
import { docsHtml, securitySchemes } from "./docs.js";
6565
/** END ROUTES */
6666

6767
export const instanceId = randomUUID();
@@ -217,20 +217,7 @@ Otherwise, email [[email protected]](mailto:[email protected]) for sup
217217

218218
openapi: "3.1.0" satisfies ZodOpenApiVersion, // If this is not specified, it will default to 3.1.0
219219
components: {
220-
securitySchemes: {
221-
bearerAuth: {
222-
type: "http",
223-
scheme: "bearer",
224-
bearerFormat: "JWT",
225-
description:
226-
"Authorization: Bearer {token}\n\nThis API uses JWT tokens issued by Entra ID (Azure AD) with the Core API audience. Tokens must be included in the Authorization header as a Bearer token for all protected endpoints.",
227-
},
228-
apiKeyAuth: {
229-
type: "apiKey",
230-
in: "header",
231-
name: "X-Api-Key",
232-
},
233-
},
220+
securitySchemes: securitySchemes as any,
234221
},
235222
},
236223
transform: fastifyZodOpenApiTransform,

src/api/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@fastify/caching": "^9.0.1",
3131
"@fastify/cors": "^11.0.1",
3232
"@fastify/static": "^8.1.1",
33+
"@fastify/swagger": "^9.5.0",
3334
"@middy/core": "^6.1.6",
3435
"@middy/event-normalizer": "^6.1.6",
3536
"@middy/sqs-partial-batch-failure": "^6.1.6",
@@ -59,15 +60,15 @@
5960
"stripe": "^18.0.0",
6061
"uuid": "^11.1.0",
6162
"zod": "^3.25.73",
62-
"zod-validation-error": "^3.3.1",
63-
"@fastify/swagger": "^9.5.0"
63+
"zod-validation-error": "^3.3.1"
6464
},
6565
"devDependencies": {
6666
"@tsconfig/node22": "^22.0.1",
6767
"@types/aws-lambda": "^8.10.149",
6868
"@types/qrcode": "^1.5.5",
6969
"esbuild-copy-static-files": "^0.1.0",
7070
"nodemon": "^3.1.10",
71-
"pino-pretty": "^13.0.0"
71+
"pino-pretty": "^13.0.0",
72+
"yaml": "^2.8.0"
7273
}
73-
}
74+
}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10422,7 +10422,7 @@ yallist@^4.0.0:
1042210422
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
1042310423
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
1042410424

10425-
yaml@^2.4.2:
10425+
yaml@^2.4.2, yaml@^2.8.0:
1042610426
version "2.8.0"
1042710427
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.8.0.tgz#15f8c9866211bdc2d3781a0890e44d4fa1a5fff6"
1042810428
integrity sha512-4lLa/EcQCB0cJkyts+FpIRx5G/llPxfP6VQU5KByHEhLxY3IJCH0f0Hy1MHI8sClTvsIb8qwRJ6R/ZdlDJ/leQ==

0 commit comments

Comments
 (0)