1.4.5 • Published 2 years ago

endpoint-builder v1.4.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Endpoint-builder

A simple tool for creating a typesafe .js file with an object that holds endpoints.

npm bundle size npm bundle size

About

Endpoint builder creates a .js file and .d.ts file of endpoints based on user config options. Endpoints can be strings or typesafe functions. Can read folder dirs for endpoints or paths can be added manually.

  • Built with maintaining endpoints across several apps in mind (fx frontend <-> backend)

Installation

npm i endpoint-builder

Add to your package.json scripts:

"scripts": {
    "build-endpoints": "endpoint-builder-init"
    ...
}

Run npm run build-endpoints

  • The first run will create a config file at the root of your project (i.e. the first folder to include node_modules) (endpoint.config.json). Run again after config to build files.

Usage

import ENDPOINTS from "endpoint-builder";

Output example

Typescript file which is compiled to js

type Example =
  | "an example of prefixing the file with additional types"
  | string;

const ENDPOINTS = {
  BACKEND: {
    GET: {
      AUTHOR: "/:id/:type",
      USER: "/:id/:type",
      POST: "/:id/:type",
    },
  },
  FRONTEND: {
    GET: {
      AUTHOR: (id: number, type: Example) => "author" + "/" + id + "/" + type,
      USER: (id: number, type: Example) => "user" + "/" + id + "/" + type,
      POST: (id: number, type: Example) => "post" + "/" + id + "/" + type,
    },
  },
};

export default ENDPOINTS;

Config options

OptionDescriptionDefaultsTypeRequired
const-nameThe name of the endpoint object.ENDPOINTSstringno
file-prefixesAdd any additional information to add to the beginning of the endpoints file such as types or imports""string[]no
slug-typeThe slug identifier that is used when endpoint suffixes are typed. Must contain *slug*.:id*slug*stringno
include-path-nameInclude the path name in the endpoint. Can be overriden by parentsfalsebooleanno
copy-to-current-directoryWill copy created files to the current writing directory paths.falsebooleanno
path-typeThe type of path the endpoint should return — function"string""string" OR "function"no
parentsNests all paths under parent keys. Useful if you need a distinction between an ENDPOINT.FRONTEND and an ENDPOINT.BACKEND. More info herenullObjectno
pathsManually add endpoint paths as string arraynullstring[]no
foldersRead a directory to get path names. Will automatically remove file extensions. More info here.nullObjectno
endpointsThe endpoint keys More info here.Objectnullyes

For autocomplete help there is a json schema which can be found here: node_modules/endpoint-builder/endpoint.schema.json. Add as below to your endpoint.config.json file.

"$schema": "node_modules/endpoint-builder/endpoint.schema.json",

Endpoint options

OptionDescriptionDefaultsTypeRequired
excludeAllWill exclude all paths.falsebooleanno
excludeWill exclude paths.[]string[]no
includeWill include paths.[]string[]no
suffixSuffixes to be added at the end of the path. Supports types IF followed by a colon: 'id:number', 'type:string', 'custom-suffix'""string[]no
customCustom paths. Will include key name as uri-path. Accepts string array of suffixesnullstring[]no

Folders options

OptionDescriptionDefaultsTypeRequired
pathThe path to the folder relative to the config file""stringno
excludeThe dir items to exclude[]string[]no
includeThe dir items to include[]string[]no

Parents options

OptionDescriptionDefaultsTypeRequired
path-typeThe type of path the endpoint should return.include-path-name (see above)"string" OR "function"no
include-path-nameInclude the path name in the endpoint.include-path-name (see above)booleanno

Example endpoint.json.config

{
  "$schema": "node_modules/endpoint-builder/endpoint.schema.json",
  "slug-type": ":*slug*",
  "include-path-name": false,
  "include-base-paths": true,
  "path-type": "string",
  "const-name": "ENDPOINTS",
  "endpoints": {
    "GET": {
      "suffix": ["id:number", "type:Example"]
    },
    "GET_ALL": {}
  },
  "file-prefixes": [
    "type Example = \"an example of prefixing the file with additional types\" | string"
  ],
  "folders": {
    "SRC": {
      "path": "backend/src"
    }
  },
  "parents": {
    "BACKEND": {
      "path-type": "string",
      "include-path-name": false
    },
    "FRONTEND": {
      "path-type": "function",
      "include-path-name": true
    }
  },
  "paths": ["user", "post"]
}
1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago