Add netlify serverless function for api

pull/2163/head
Jérémy Rialland 2022-05-23 10:02:19 +02:00 committed by Johan Girod
parent 3dc4226af1
commit 0a7014bb89
8 changed files with 687 additions and 153 deletions

View File

@ -3,16 +3,19 @@
"license": "MIT",
"version": "0.0.0",
"description": "API du site mon-entreprise",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"validate": "yarn swagger-cli validate ./source/openapi.yaml",
"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",
"start": "yarn clean && yarn build:watch & wait-on ./dist && nodemon -d 1s ./serverless.ts",
"build": "yarn build:openapi && yarn build:tsup",
"build:watch": "yarn build:openapi:watch & yarn wait:openapi && yarn build:tsup:watch",
"build:ts": "tsc",
"build:ts:watch": "yarn build:ts -w --preserveWatchOutput",
"build:tsup": "tsup-node",
"build:tsup:watch": "run build:tsup --watch",
"copy:openapi": "cp ./source/openapi.json ./dist",
"wait:openapi": "wait-on ./source/openapi.json",
"build:openapi": "yarn run swagger-cli bundle ./source/openapi.yaml > ./source/openapi.json",
"build:openapi:watch": "nodemon -d 500ms -w ./source/openapi.yaml -x \"yarn build:openapi\"",
@ -26,23 +29,28 @@
"dependencies": {
"@koa/cors": "^3.3.0",
"@koa/router": "^10.1.1",
"@publicodes/api": "^1.0.0-beta.36",
"koa": "^2.13.4",
"koa-body": "^5.0.0",
"koa-static": "^5.0.0",
"modele-social": "portal:/home/wiinxt/mon-entreprise/modele-social",
"publicodes": "^1.0.0-beta.35",
"publicodes-api": "portal:/home/wiinxt/publicodes/packages/api",
"modele-social": "workspace:^",
"publicodes": "^1.0.0-beta.36",
"serverless-http": "^3.0.1",
"swagger-ui-dist": "^4.11.0"
},
"devDependencies": {
"@apidevtools/swagger-cli": "^4.0.4",
"@netlify/functions": "^1.0.0",
"@types/koa": "^2.13.4",
"@types/koa-static": "^4.0.2",
"@types/koa__cors": "^3.3.0",
"@types/koa__router": "^8.0.11",
"@types/node": "^17.0.32",
"@types/swagger-ui-dist": "^3.30.1",
"nodemon": "^2.0.15",
"nodemon": "^2.0.16",
"serverless-http": "^3.0.1",
"ts-node": "^10.7.0",
"tsup": "^5.12.8",
"typescript": "4.7.1-rc",
"wait-on": "^6.0.1"
}

8
api/serverless.ts Normal file
View File

@ -0,0 +1,8 @@
import serverless from 'serverless-http'
import { app } from './source/index.cjs'
const handler = serverless(app, {
provider: 'aws',
})
export { handler }

View File

@ -1,11 +1,11 @@
import cors from '@koa/cors'
import Router from '@koa/router'
import { koaMiddleware as publicodesAPI } from '@publicodes/api'
import { readFileSync } from 'fs'
import Koa from 'koa'
import rules from 'modele-social'
import path from 'path'
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'
@ -20,7 +20,11 @@ app.use(cors())
const apiRoutes = publicodesAPI(() => new Engine(rules))
router.use('/v1', apiRoutes, docRoutes(), await openapiRoutes(openapi))
const openapi = JSON.parse(
// eslint-disable-next-line no-undef
readFileSync(path.resolve(__dirname, 'openapi.json'), { encoding: 'utf8' })
) as Record<string, unknown>
router.use('/api/v1', apiRoutes, docRoutes(), openapiRoutes(openapi))
app.use(router.routes())
app.use(router.allowedMethods())
@ -31,3 +35,5 @@ app.listen(port, function () {
// eslint-disable-next-line no-console
console.log('listening on port:', port)
})
export { app }

View File

@ -1,18 +1,15 @@
import Router from '@koa/router'
import { Context } from 'koa'
import { openapi } from 'publicodes-api'
import { openapi as publicodesOpenapi } from '@publicodes/api'
import { mergeDeep } from '../utils.js'
/**
* /openapi.json route, merge customOpenapi with publicodes-api openapi json
* /openapi.json route, merge customOpenapi with @publicodes/api openapi json
* @param customOpenapi
* @returns
*/
export const openapiRoutes = async (
customOpenapi?: Record<string, unknown>
) => {
export const openapiRoutes = (customOpenapi?: Record<string, unknown>) => {
const router = new Router()
const publicodesOpenapi = await openapi()
const mergedOpenapi = customOpenapi
? mergeDeep(publicodesOpenapi, customOpenapi)

View File

@ -1,8 +1,8 @@
{
"compilerOptions": {
/* Basic Options */
"incremental": true,
"target": "ES2022",
// "incremental": true, // disabled for tsup, see https://github.com/egoist/tsup/issues/615 for implementation status
"target": "ES2020",
"module": "Node16",
"outDir": "dist",
"declaration": true,
@ -25,6 +25,7 @@
/* Module Resolution Options */
"moduleResolution": "Node16",
"isolatedModules": true,
"esModuleInterop": true,
"resolveJsonModule": true,
@ -34,6 +35,6 @@
"ts-node": {
"esm": true
},
"include": ["source"],
"include": ["source", "serverless.ts"],
"exclude": ["**/node_modules", "**/dist", "**/*.test.*"]
}

14
api/tsup.config.ts Normal file
View File

@ -0,0 +1,14 @@
import { defineConfig } from 'tsup'
export default defineConfig([
{
entry: {
index: 'serverless.ts',
},
format: ['cjs'],
target: 'es2020',
clean: true,
dts: true,
onSuccess: 'yarn copy:openapi',
},
])

View File

@ -37,11 +37,11 @@
},
"packageManager": "yarn@3.2.0",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"eslint": "^8.12.0",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"eslint": "^8.15.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0-1",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-cypress": "^2.12.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-mocha": "^10.0.3",

750
yarn.lock

File diff suppressed because it is too large Load Diff