# ts-template-api-express

> Template API Typescript

Latest version **1.0.1** (published 2022-11-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install ts-template-api-express
pnpm add ts-template-api-express
yarn add ts-template-api-express
bun add ts-template-api-express
```

Provides the command `ts-template-api-express`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-11-23 |
| First published | 2022-11-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Sergio González |
| Maintainers | jscode-es |

## Links

- npm: https://www.npmjs.com/package/ts-template-api-express
- Repository: https://github.com/jscode-es/ts-template-api-express
- Homepage: https://github.com/jscode-es/ts-template-api-express#readme
- Issues: https://github.com/jscode-es/ts-template-api-express/issues
- npm.io page: https://npm.io/package/ts-template-api-express

## Dependencies (4)

- [cors](https://npm.io/package/cors.md) 2.8.5
- [dotenv](https://npm.io/package/dotenv.md) 16.0.1
- [express](https://npm.io/package/express.md) 4.18.1
- [prompts](https://npm.io/package/prompts.md) ^2.4.2

## Recent versions

- 1.0.1 (latest) — 2022-11-23
- 1.0.0 — 2022-11-23

## README

# 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.

```javascript

import { IReturn, IObject, IData } from '../types'

export const getData ({ data, req }:IData): Promise<IReturn>

```

### Example static
```javascript

// 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
```javascript

// 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
```javascript

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
}


```

---
_Source: https://npm.io/package/ts-template-api-express · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
