-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: set CORS URLs endpoint * remove debug
- Loading branch information
Showing
5 changed files
with
77 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { FastifyInstance } from "fastify"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration"; | ||
import { getConfig } from "../../../../utils/cache/getConfig"; | ||
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas"; | ||
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls"; | ||
import { ReplySchema } from "./get"; | ||
|
||
const BodySchema = Type.Object({ | ||
urls: Type.Array( | ||
Type.String({ | ||
description: "Comma separated list of origins that will call Engine", | ||
minLength: 1, | ||
}), | ||
), | ||
}); | ||
|
||
BodySchema.examples = [ | ||
{ | ||
urls: ["https://example.com", "https://subdomain.example.com"], | ||
}, | ||
]; | ||
|
||
export async function setUrlsToCorsConfiguration(fastify: FastifyInstance) { | ||
fastify.route<{ | ||
Body: Static<typeof BodySchema>; | ||
Reply: Static<typeof ReplySchema>; | ||
}>({ | ||
method: "PUT", | ||
url: "/configuration/cors", | ||
schema: { | ||
summary: "Set CORS URLs", | ||
description: | ||
"Replaces the CORS URLs to allow client-side calls to Engine", | ||
tags: ["Configuration"], | ||
operationId: "setUrlsToCorsConfiguration", | ||
body: BodySchema, | ||
response: { | ||
...standardResponseSchema, | ||
[StatusCodes.OK]: ReplySchema, | ||
}, | ||
}, | ||
handler: async (req, res) => { | ||
const urls = req.body.urls.map((url) => url.trim()); | ||
|
||
// Add required domains and dedupe. | ||
const dedupe = Array.from( | ||
new Set([...urls, ...mandatoryAllowedCorsUrls]), | ||
); | ||
|
||
await updateConfiguration({ | ||
accessControlAllowOrigin: dedupe.join(","), | ||
}); | ||
|
||
// Fetch and return the updated configuration | ||
const config = await getConfig(false); | ||
res.status(200).send({ | ||
result: config.accessControlAllowOrigin.split(","), | ||
}); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters