Skip to content

Instantly share code, notes, and snippets.

@xasima
Last active April 22, 2021 13:47
Show Gist options
  • Select an option

  • Save xasima/f4b54a092cb046d02b5d595922936322 to your computer and use it in GitHub Desktop.

Select an option

Save xasima/f4b54a092cb046d02b5d595922936322 to your computer and use it in GitHub Desktop.
Calculator-react-sample
// calculator-api-swagger/generator.js
var fs = require('fs');
var CodeGen = require('swagger-js-codegen').CodeGen;
var file = 'api/swagger/swagger.json';
var swagger = JSON.parse(fs.readFileSync(file, 'UTF-8'));
var reactjsSourceCode = CodeGen.getReactCode({ className: 'Test', swagger: swagger });
console.log(reactjsSourceCode);
//var tsSourceCode = CodeGen.getTypescriptCode({ className: 'Test', swagger: swagger, imports: ['../../typings/tsd.d.ts'] });
//var nodejsSourceCode = CodeGen.getNodeCode({ className: 'Test', swagger: swagger });
//var angularjsSourceCode = CodeGen.getAngularCode({ className: 'Test', swagger: swagger });
//console.log(tsSourceCode);
// calculator-api-express/api.js
// only part of the file, see line numbers in comments
const routes = require('./routes/index'); //12
const calculator = require('./routes/calculator'); //13
app.use('/', routes); //39
app.use('/api/v1', calculator); //40
// calculator-api-express/routes/calculator.js (renamed from routes/users.js)
const express = require('express');
const router = express.Router();
var faker = require('faker');
/* GET users listing. */
router.get('/', (req, res) => {
res.send('API for calculator');
});
router.get('/plus', (req, res) => {
res.send({'result': faker.random.number()});
});
module.exports = router;
// Cloned react project
git clone https://github.com/ahfarmer/calculator
// Install Atom and plugins: nuclide, nuclide-format-js, nuclide-esformatter, flow-ide
// Open React project and examine code
// Create Swagger API defintion on top-level, pass proper project name in creation procedure
swagger project create
swagger project edit calculator-api-swagger
// Take a look at the swagger.yaml attached below
// Install express project with basic params: no MVC, handlebars, no css preprocessor
yo express
// Implement changes in Express project: rename routes/user.js, make changes in accordance with files
cd calculator-api-express
npm install --save faker
npm start
// Check http://localhost:3000/api/v1/plus?param1=1&param2=2
// Check folders
ls -la | grep calculator
>> calculator
>> calculator-api-express
>> calculator-api-swagger
// Create JSON for swagger API
cd calculator-api-swagger
mkdir codegen
swagger-codegen generate -i api/swagger/swagger.yaml -l swagger -o codegen
or
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate \
-i /local/api/swagger/swagger.yaml \
-l swagger \
-o /local/codegen
swagger: "2.0"
info:
version: "0.0.1"
title: Calculator App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /api/v1
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/plus:
# binds a127 app logic to a route
x-swagger-router-controller: calculator_plus
get:
description: Returns result to the caller of a+b
# used as the method name of the controller
operationId: plus
parameters:
- name: param1
in: query
description: The name of the first param
required: false
type: number
- name: param2
in: query
description: The name of the second param
required: false
type: number
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/ResultResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
ResultResponse:
required:
- result
properties:
result:
type: number
ErrorResponse:
required:
- message
properties:
message:
type: string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment