This is a
Frontend-library
, for receiving APIs generated by Nest API Generator
For
Backend-developer
, please see Nest API Generator
A Code-generation script to easily create all AJAX files corresponding to your backend !
NO
specific AJAX library required, you can use any if you want- One-click code generation, you just need to provide the directories
TypeScript
Only
$ npm install api-code-generator
// or
$ yarn add api-code-generator
Outside your source folder, create a script
folder, then create a file call api.ts
(or anything you want)
Remember to add a
tsconfig.json
to include codes inscript
folder
import { APIGeneratorBase } from 'api-code-generator';
import path from 'path';
import axios from 'axios';
/**
* @param {string} metadataEndpointURL
* @param {string} typeFilePath
* @param {string} serviceFolderPath
* @param {PlatformConfig} platformConfig
*
* PlatformConfig
* @param {Function} ajaxFunction
* @param {string} ajaxFunctionImportStatement
* @param {string} typeFileImportPath
*/
new APIGeneratorBase({
metadataEndpointURL: 'Get Request to the backend',
typeFilePath: path.join(__dirname, '../src/type/api.ts'),
serviceFolderPath: path.join(__dirname, '../src/util/service'),
platformConfig: {
ajaxFunction,
ajaxFunctionImportStatement: 'import {ajaxFunction} from "somewhere"',
typeFileImportPath: 'util/type', // any path relative to service folder
},
}).run();
/**
* @param {'GET' | 'DELETE' | 'PUT' |'POST'} method
* @param {string} endpoint
* @param {string} requestParam
* @param {'request' | 'null'} request
*/
const ajaxFunction = (method, endpoint, requestParam, request) => {
/**
* Example: GET /v1/my/endpoint/:id/user?username="my-name"&age=10
*
* method: 'GET'
* endpoint: '/v1/my/endpoint/:id/user
* requestParam: 'id',
* request: 'request' <- refer to a variable, use that as request body
*/
/**
* Example
*
* axios({
* url: endpoint + requestParam,
* data: request,
* method: method
* })
*/
return `your statement to perform ajax call`;
};
* my_awesome_project
|
+-- script
| +-- api.ts
+-- src
| +-- util
| | +-- service
| | | +-- AuthAJAXService.ts // Generated service file
| +-- type
| | +-- api.ts // Type Definition File