Skip to content

Commit

Permalink
feat: upload openapi info from external file (#410)
Browse files Browse the repository at this point in the history
* feat: upload openapi info from external file

* test: update cli tests

* docs: typo
  • Loading branch information
matteo-cristino authored Dec 23, 2024
1 parent 95ebc51 commit c830ea2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later

- [🎮 Quick start](#-quick-start)
- [💾 Build](#-build)
- [💾🪫 Build on ARM32](#-build-on-arm32)
- [🐋 Docker](#-docker)
- [🔧 Configuration](#-configuration)
- [📋 Testing](#-testing)
Expand Down Expand Up @@ -130,6 +131,9 @@ Options:
* `--openapi-path <string>`
env: **OPENAPI_PATH**
specify where to mount the swagger docs (default: "/docs")
* `--openapi-info <file>`
env: **OPENAPI_INFO**
provide the json info for the swagger docs (default: "./openapi_info.json")
* `--hostname <string>`
env: **HOSTNAME**
Provide the hostname to serve the server (default: "0.0.0.0")
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ program
.env('OPENAPI_PATH')
.default('/docs')
)
.addOption(
new Option('--openapi-info <file>', 'Provide the json info for the swagger docs')
.env('OPENAPI_INFO')
.default('./openapi_info.json')
)
.addOption(
new Option('--hostname <string>', 'specify the hostname to serve the server')
.env('HOSTNAME')
Expand Down
46 changes: 29 additions & 17 deletions src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { OpenAPIV3_1 } from 'openapi-types';
import p from '../package.json' with { type: 'json' };
import { JSONSchema, Metadata } from './types.js';
import { config } from './cli.js';
import { readFileSync } from 'fs';

export const defaultTagsName = {
zen: '📑 Zencode APIs',
Expand Down Expand Up @@ -119,30 +120,41 @@ export const defaultTags = [
}
];

export const definition: Partial<OpenAPIV3_1.Document> = {
openapi: '3.1.0',
paths: {},
info: {
title: 'noˑcodeˑroom',
version: p.version,
description: `## Restful api from zencode smart contracts with no code!
const openapiInfo = {
title: 'noˑcodeˑroom',
version: p.version,
description: `## Restful api from zencode smart contracts with no code!
This is a simple API autogenerated from a folder within your server.
To add new endpoints you should add new zencode contracts in the directory.
**NB** The files should be in form of \`endpoint.zen\` then your contract will run on \`/endpoint\``,
termsOfService: 'https://forkbomb.solutions/privacy-policy/',
contact: {
email: '[email protected]',
name: 'Forkbomb BV',
url: 'https://forkbomb.solutions'
},
license: {
name: 'GNU Affero General Public License v3.0 or later',
url: 'https://www.gnu.org/licenses/agpl-3.0'
}
termsOfService: 'https://forkbomb.solutions/privacy-policy/',
contact: {
email: '[email protected]',
name: 'Forkbomb BV',
url: 'https://forkbomb.solutions'
},
license: {
name: 'GNU Affero General Public License v3.0 or later',
url: 'https://www.gnu.org/licenses/agpl-3.0'
}
};

function getOpenapiInfo(): OpenAPIV3_1.InfoObject {
try {
const content = readFileSync(config.openapiInfo, 'utf-8');
return Object.assign({}, openapiInfo, JSON.parse(content));
} catch {
return openapiInfo;
}
}

export const definition: Partial<OpenAPIV3_1.Document> = {
openapi: '3.1.0',
paths: {},
info: getOpenapiInfo(),
tags: []
};

Expand Down
1 change: 1 addition & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const res = {
basepath: '',
debug: false,
hostname: '0.0.0.0',
openapiInfo: './openapi_info.json',
openapiPath: '/docs',
port: 0,
template: './applet_template.html',
Expand Down

0 comments on commit c830ea2

Please sign in to comment.