diff --git a/api/package.json b/api/package.json index ec05dbf6c..62423d768 100644 --- a/api/package.json +++ b/api/package.json @@ -8,14 +8,14 @@ "type": "module", "scripts": { "validate": "yarn swagger-cli validate ./source/openapi.yaml", - "start": "yarn build:watch & wait-on ./dist && NODE_OPTIONS=--experimental-json-modules nodemon ./source/index.ts", + "start": "yarn clean && yarn build:watch & wait-on ./dist && NODE_OPTIONS=--experimental-json-modules nodemon -d 1s ./source/index.ts", "build": "yarn build:openapi && yarn build:ts", "build:watch": "yarn build:openapi:watch & yarn wait:openapi && yarn build:ts:watch --preserveWatchOutput", "build:ts": "NODE_OPTIONS=--experimental-json-modules tsc", "build:ts:watch": "yarn build:ts -w", "wait:openapi": "wait-on ./source/openapi.json", "build:openapi": "yarn run swagger-cli bundle ./source/openapi.yaml > ./source/openapi.json", - "build:openapi:watch": "nodemon -w ./source/openapi.yaml -x \"yarn build:openapi\"", + "build:openapi:watch": "nodemon -d 500ms -w ./source/openapi.yaml -x \"yarn build:openapi\"", "clean": "rm -rf dist ./source/openapi.json" }, "repository": { diff --git a/api/source/index.ts b/api/source/index.ts index 9cadb66b0..aa70056d5 100644 --- a/api/source/index.ts +++ b/api/source/index.ts @@ -4,8 +4,10 @@ import Koa from 'koa' import rules from 'modele-social' import Engine from 'publicodes' import { koaMiddleware as publicodesAPI } from 'publicodes-api' +// @ts-ignore import openapi from './openapi.json' import { docRoutes } from './route/doc.js' +import { openapiRoutes } from './route/openapi.js' type State = Koa.DefaultState @@ -16,11 +18,9 @@ const router = new Router() app.use(cors()) -const apiRoutes = publicodesAPI(() => new Engine(rules), { - customOpenapi: openapi, -}) +const apiRoutes = publicodesAPI(() => new Engine(rules)) -router.use('/v1', apiRoutes, docRoutes()) +router.use('/v1', apiRoutes, docRoutes(), await openapiRoutes(openapi)) app.use(router.routes()) app.use(router.allowedMethods()) diff --git a/api/source/route/openapi.ts b/api/source/route/openapi.ts new file mode 100644 index 000000000..48e97f1f5 --- /dev/null +++ b/api/source/route/openapi.ts @@ -0,0 +1,27 @@ +import Router from '@koa/router' +import { Context } from 'koa' +import { openapi } from 'publicodes-api' +import { mergeDeep } from '../utils.js' + +/** + * /openapi.json route, merge customOpenapi with publicodes-api openapi json + * @param customOpenapi + * @returns + */ +export const openapiRoutes = async ( + customOpenapi?: Record +) => { + const router = new Router() + const publicodesOpenapi = await openapi() + + const mergedOpenapi = customOpenapi + ? mergeDeep(publicodesOpenapi, customOpenapi) + : publicodesOpenapi + + router.get('/openapi.json', (ctx: Context) => { + ctx.type = 'application/json' + ctx.body = mergedOpenapi + }) + + return router.routes() +} diff --git a/api/source/utils.ts b/api/source/utils.ts new file mode 100644 index 000000000..45417e99c --- /dev/null +++ b/api/source/utils.ts @@ -0,0 +1,41 @@ +/** + * Simple object check. + * @param item + * @returns {boolean} + */ +export function isObject(item: unknown): item is Record { + return !!item && typeof item === 'object' && !Array.isArray(item) +} + +/** + * Deep merge two objects. + * @param target + * @param ...sources + */ +export function mergeDeep( + target: Record, + ...sources: Record[] +): Record { + if (!sources.length) { + return target + } + const source = sources.shift() + + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) { + Object.assign(target, { [key]: {} }) + } + mergeDeep( + target[key] as Record, + source[key] as Record + ) + } else { + Object.assign(target, { [key]: source[key] }) + } + } + } + + return mergeDeep(target, ...sources) +} diff --git a/api/tsconfig.json b/api/tsconfig.json index 5e66a0b68..397d5b4b3 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -24,7 +24,7 @@ "noFallthroughCasesInSwitch": true, /* Module Resolution Options */ - "moduleResolution": "Node", + "moduleResolution": "NodeNext", "esModuleInterop": true, "resolveJsonModule": true,