Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
✨ (openapi) Preparing for CodeGen
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Sep 11, 2019
1 parent 49bb0c7 commit f5b9f18
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 99 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ All **HospitalRun** code lives in a single repository, an architecture generally

We recommend the use of [**nvm**](https://github.com/nvm-sh/nvm#install--update-script) for the management of different versions of Node.

#### pnpm
Fast, disk space efficient package manager
`npm i -g pnpm`

#### yarn
Fast, reliable, and secure dependency management.
`npm i -g yarn`
Expand All @@ -27,8 +23,9 @@ Fast, reliable, and secure dependency management.
```
git clone [email protected]:HospitalRun/hospitalrun.git
git submodule update --init --recursive
npx pnpm install -r --filter @hospitalrun-org/cli
npx pnpm install -r
yarn
yarn workspaces run build
yarn upgrade
# Do some coding then commit with
npx git-cz
# Test the cli
Expand Down
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
"author": "Maksim Sink <[email protected]>",
"license": "MIT",
"private": true,
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/cz-emoji",
"**/cz-emoji/**",
"**/husky",
"**/husky/**"
]
},
"scripts": {
"test": "exit 0",
"clean": "rimraf packages/*/node_modules packages/*/dist packages/*/build node_modules",
"download": "wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.8/swagger-codegen-cli-2.4.8.jar -d ./bin/",
"build:core": "npm run download && npm run build:swagger && java -Dmodels -jar ./bin/swagger-codegen-cli-2.4.8.jar generate -i ./build/swagger.yml -l typescript-inversify -o ./packages/core",
"download": "mkdirp bin && wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.8/swagger-codegen-cli-2.4.8.jar -d ./bin/",
"build:core": "npm run build:core-services && npm run build:core-models",
"build:swagger": "mkdirp ./build && swagger-merger -i ./spec/next/index.yml -o ./build/swagger.yml",
"build:fastify": "npm run build:swagger && snc -t ./templates/fastify -o ./packages/server ./build/swagger.yml"
"build:fastify": "npm run build:swagger && snc -t ./templates/fastify -o ./packages/server/src ./build/swagger.yml",
"build:core-services": "rimraf ./packages/core/src/index.ts && npm run build:swagger && snc -t ./templates/inversify -o ./packages/core/src ./build/swagger.yml",
"build:core-models": "npm run download && npm run build:swagger && java -Dmodels -jar ./bin/swagger-codegen-cli-2.4.8.jar generate -i ./build/swagger.yml -l typescript-inversify -o ./packages/core/src"
},
"devDependencies": {
"@commitlint/cli": "^8.1.0",
Expand All @@ -26,6 +39,7 @@
"cz-emoji": "^1.1.2",
"dateformat": "^3.0.3",
"husky": "^3.0.5",
"jest": "24.8.0",
"mkdirp": "^0.5.1",
"node-wget": "^0.4.3",
"pnpm": "^3.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli
Submodule cli updated from 78f6cf to 40694d
2 changes: 1 addition & 1 deletion packages/core
2 changes: 1 addition & 1 deletion packages/docs
Submodule docs updated from 57243e to a35050
7 changes: 0 additions & 7 deletions pnpm-workspace.yaml

This file was deleted.

48 changes: 48 additions & 0 deletions spec/next/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
swagger: '2.0'
info:
version: 2.0.0
title: HospitalRun
description: +HospitalRun API
license:
name: MIT
host: app.hospitalrun.io
basePath: "/next"
securityDefinitions:
couchBasicAuth:
type: basic
couchCookieAuth:
type: apiKey
name: Cookie
in: header
couchProxyAuth:
type: apiKey
name: X-Auth-CouchDB-Token
in: header
couchProxyAuthRoles:
type: apiKey
name: X-Auth-CouchDB-Roles
in: header
couchProxyAuthUsername:
type: apiKey
name: X-Auth-CouchDB-UserName
in: header

security:
- couchBasicAuth: []
- couchCookieAuth: []
- couchProxyAuth: []
- couchProxyAuthRoles: []
- couchProxyAuthUsername: []

schemes:
- https
consumes:
- application/json
produces:
- application/json
parameters:
$ref#Parameters: "./parameters.yml"
paths:
$ref#UsersMethods: "./users/methods.yml"
definitions:
$ref#UsersModels: "./users/models.yml"
2 changes: 1 addition & 1 deletion spec/next/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ consumes:
produces:
- application/json
parameters:
$ref#UsersParameters: "./users/parameters.yml"
$ref#Parameters: "./parameters.yml"
paths:
$ref#UsersMethods: "./users/methods.yml"
definitions:
Expand Down
6 changes: 6 additions & 0 deletions spec/next/parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id:
in: path
name: id
type: string
required: true
description: ID of an Document to Get
28 changes: 21 additions & 7 deletions spec/next/users/methods.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/users:
/user:
get:
tags:
- Users
operationId: findUsers
- User
operationId: findUser
summary: Finds a User
description: Search for a User in the Database
responses:
"200":
description: Returns a list of Users
description: Returns a User
schema:
type: array
items:
$ref: "#/definitions/User"
$ref: "#/definitions/User"
/user/{id}:
get:
security:
- couchBasicAuth: []
tags:
- User
operationId: findUserById
summary: Finds a User
parameters:
- $ref: '#/parameters/id'
description: Search for a User in the Database
responses:
"200":
description: Returns a User
schema:
$ref: "#/definitions/User"
6 changes: 4 additions & 2 deletions spec/next/users/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ User:
type: string
last_name:
type: string
username:
type: string
id:
type: string
required:
- id
6 changes: 0 additions & 6 deletions spec/next/users/parameters.yml

This file was deleted.

29 changes: 0 additions & 29 deletions templates/fastify/plugins/___.js

This file was deleted.

38 changes: 38 additions & 0 deletions templates/fastify/plugins/___.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fp from 'fastify-plugin'
import { {{capitalize operation_name}}Service, {{capitalize operation_name}} } from '@hospitalrun-org/core'

export default fp((fastify, opts, next) => {
const {{camelCase operation_name}}Service = new {{capitalize operation_name}}Service(opts)
{{#each operation}}
{{#each this.path}}
{{#validMethod @key}}
/**
* @param {Object} options
{{#each ../parameters}}
{{#if this.name}}
* @param {{../../../../openbrace}}{{capitalize type}}{{../../../../closebrace}} options.{{name}} {{inline description}}
{{/if}}
{{/each}}
* @throws {Error}
* @return {Promise}
*/
fastify.decorate('{{../operationId}}', ({{camelCase ../../../operation_name}}: {{capitalize ../../../operation_name}}) => {
return {{camelCase ../../../operation_name}}Service.{{../operationId}}({{camelCase ../../../operation_name}})
})
{{/validMethod}}
{{/each}}
{{/each}}
next()
})

declare module 'fastify' {
interface FastifyInstance {
{{#each operation}}
{{#each this.path}}
{{#validMethod @key}}
{{../operationId}}(): {{capitalize ../../../operation_name}}
{{/validMethod}}
{{/each}}
{{/each}}
}
}
111 changes: 111 additions & 0 deletions templates/fastify/services/routes/___.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyInstance } from 'fastify'
import { nextCallback } from 'fastify-plugin'

export default(
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
opts: {},
next: nextCallback,
) => {
{{#each headOperation}}
{{#each this.path}}
{{#validMethod @key}}
/**
{{#each ../descriptionLines}}
* {{{this}}}
{{/each}}
*/
fastify.{{@key}}('{{../../subresource}}', async (request, reply) => {
const options = {
{{#if ../requestBody}}
body: request.body{{#compare (lookup ../parameters 'length') 0 operator = '>' }},{{/compare}}
{{/if}}
{{#each ../parameters}}
{{#equal this.in "query"}}
{{{quote ../name}}}: request.query['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{#equal this.in "path"}}
{{{quote ../name}}}: request.params['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{#equal this.in "header"}}
{{{quote ../name}}}: request.header['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{/each}}
};
console.log(options)
try {
const result = await fastify.{{../operationId}}();
{{#ifNoSuccessResponses ../responses}}
reply.header('X-Result', result.data).code(200).send();
{{else}}
reply.code(result.code || 200).send(result);
{{/ifNoSuccessResponses}}
} catch (err) {
{{#ifNoErrorResponses ../responses}}
reply.code(500).send({
status: 500,
error: 'Server Error'
});
{{else}}
next(err);
{{/ifNoErrorResponses}}
}
});
{{/validMethod}}
{{/each}}
{{/each}}

{{#each operation}}
{{#each this.path}}
{{#validMethod @key}}
/**
{{#each ../descriptionLines}}
* {{{this}}}
{{/each}}
*/
fastify.{{@key}}('{{../../subresource}}', async (request, reply) => {
const options = {
{{#if ../requestBody}}
body: request.body{{#compare (lookup ../parameters 'length') 0 operator = '>' }},{{/compare}}
{{/if}}
{{#each ../parameters}}
{{#equal this.in "query"}}
{{{quote ../name}}}: request.query['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{#equal this.in "path"}}
{{{quote ../name}}}: request.params['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{#equal this.in "header"}}
{{{quote ../name}}}: request.header['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{#match @../key "(post|put)"}}
{{#equal ../in "body"}}
{{{quote ../name}}}: request.body['{{../name}}']{{#unless @last}},{{/unless}}
{{/equal}}
{{/match}}
{{/each}}
};
console.log(options)
try {
const result = await fastify.{{../operationId}}();
{{#ifNoSuccessResponses ../responses}}
reply.code(200).send(result);
{{else}}
reply.code(200).send(result);
{{/ifNoSuccessResponses}}
} catch (err) {
{{#ifNoErrorResponses ../responses}}
reply.code(500).send({
status: 500,
error: 'Server Error'
});
{{else}}
next(err);
{{/ifNoErrorResponses}}
}
});
{{/validMethod}}
{{/each}}
{{/each}}
next()
}
Loading

0 comments on commit f5b9f18

Please sign in to comment.