Skip to content

Jamyth/api-code-generator

Repository files navigation

API Code Generator

Notice

This is a Frontend-library, for receiving APIs generated by Nest API Generator

For Backend-developer, please see Nest API Generator

Introduction

A Code-generation script to easily create all AJAX files corresponding to your backend !

Feature

  • 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

Installation

$ npm install api-code-generator

// or

$ yarn add api-code-generator

Usage

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 in script 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`;
};

Folder Structure (DEMO)

* my_awesome_project
|
+-- script
|   +-- api.ts
+-- src
|   +-- util
|   |   +-- service
|   |   |   +-- AuthAJAXService.ts // Generated service file
|   +-- type
|   |   +-- api.ts // Type Definition File

About

An API Code Generator for generating files with the result created by nest-api-generator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published