UNPKG

z-openapi

Version:

Library to help writing swagger file definition

108 lines 4.12 kB
"use strict"; /** * * Copyright (c) 2020, Benning Samuel * * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, * provided that the above copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Source: http://opensource.org/licenses/ISC * * */ Object.defineProperty(exports, "__esModule", { value: true }); const lib_1 = require("../lib"); function basic() { /** * create an Zopenapi instance */ const zoa = new lib_1.Zopenapi(); /** * Set top level informations */ zoa.info(info => info .title('Basic Title') .version('1.0.0') .description('Basic Description') .contact(contact => contact.email('samuel.benning@zento.fr')) .license(license => license.name('ISC'))); /** * Set server's urls */ zoa.server({ url: 'http://localhost:3000' }); /** * * Set top level components: * * - Security schemes (bearer, apikey, ...) * * - Base schemas (BaseError, ...) * */ zoa.components(components => components .securitySchemes({ bearer: bearer => bearer.name('Authorization').in('header'), apikey: apikey => apikey.name('api-key').in('query'), }) .schemas({ BaseError: schema => schema.object({ name: name => name.string(true), message: message => message.string(true), }) })); /** * Add more schemas in your model's modules */ zoa.componentsSchemas({ User: user => user.object({ id: id => id.string(), email: email => email.string(true), password: password => password.string(), }), Users: users => users.array(items => items.ref('User')), CreateUser: user => user.object({ email: email => email.string(true), password: password => password.string(true), }), UpdateUser: user => user.object({ email: email => email.string(), password: password => password.string(), }) }); /** * Add pathes in your router's modules */ zoa.paths({ '/users': path => path .get(get => get .response200(resp => resp.jsonSchema(json => json.ref('Users'))) .security({ bearer: [] })) .post(post => post .requestBody(body => body.jsonSchema(json => json.ref('CreateUser'))) .response200(resp => resp.jsonSchema(json => json.ref('User'))) .security({ apikey: [] })), '/users/{id}': path => path .get(get => get .parameters(param => param.name('id')) .response200(resp => resp.jsonSchema(json => json.ref('User'))) .security({ bearer: [] })) .put(put => put .parameters(param => param.name('id')) .requestBody(body => body.jsonSchema(json => json.ref('UpdateUser'))) .response200(resp => resp.jsonSchema(json => json.ref('User'))) .security({ bearer: [] })) .delete(del => del .parameters(param => param.name('id')) .response200(resp => resp.jsonSchema(json => json.ref('User'))) .security({ bearer: [] })) }); console.log('Plain javascript object: ', zoa.js()); console.log('Stringified POJO: ', zoa.json()); return zoa; } exports.basic = basic; //# sourceMappingURL=basic.js.map