npm.io
1.0.1 β€’ Published 3 years agoCLI

ts-template-api-express

Licence
ISC
Version
1.0.1
Deps
4
Size
15 kB
Vulns
2
Weekly
0

Template API ExpressJS ( Typescript )

Template of a server to offer a consumable service of a Rest API

Module installation

npx ts-template-api-express

Basic use

In the server folder there is "router", a folder to manage all web requests ( get, post, put, delete ). Within each folder you can establish the consumption of static and dynamic routes.

πŸ“‚ server
	πŸ“‚ router
		πŸ“ get
		πŸ“ post
		πŸ“ put
		πŸ“ delete

Static routes

Static routes are for direct consumption without passing an identifier in the route.

Example
πŸ“‚ server
	πŸ“‚ router
		πŸ“ get
			πŸ“ product
				index.ts

Dynamic routes

Dynamic routes are used to pass the special identifier in the url. All dynamic routes are declared as follows, [id].ts

Example
πŸ“‚ server
	πŸ“‚ router
		πŸ“ get
			πŸ“ product
				[id].ts

As the mandatory return data function declares.

In both cases, both in dynamic and static routes, the function must be declared as follows.


import { IReturn, IObject, IData } from '../types'

export const getData ({ data, req }:IData): Promise<IReturn>
Example static

// FILE_NAME: server/router/get/product/index.ts

import db from 'object_mysql'

import { IReturn, IObject, IData } from '../types'

export const getData = async ({ data, req }:IData): Promise<IReturn>
{
	const { Product } = await db()

	const { error, result } = await Product.get({limit:10})

	return { status:200, error, result}
}
Example dynamic

// FILE_NAME: server/router/get/product/[id].ts

import db from 'object_mysql'

import { IReturn, IObject, IData } from '../types'

export const getData = async ({ data, req }:IData): Promise<IReturn>
{
	const { id } = data

	const { Product } = await db()

	const { error, result } = await Product.getById(id)

	return { status:200, error, result}
}

Module typing

Below is the typing of the modules


import { Request } from 'express'

export interface IObject {
	[key: string]: string | number | boolean
}

export interface IRequest extends Request {
	isJsonRequest?: boolean;
	ipClient?: string | string[];
	data?: IObject;
	lang?: string;
}

export interface ICustomEnv extends NodeJS.ProcessEnv {
	port?: string;
	host?: string;
	limit_json?: string;
}

export interface IReturn 
{
	status:number // Http status code
	error?:string[]
	result?:string|number|boolean|IObject
}

export interface IData 
{
	data:IObject
	request:IRequest
}