Merge pull request #31107 from jeremyben/master

Add types for json-server
This commit is contained in:
Andrew Casey
2018-12-06 14:51:51 -08:00
committed by GitHub
4 changed files with 130 additions and 0 deletions

72
types/json-server/index.d.ts vendored Normal file
View File

@@ -0,0 +1,72 @@
// Type definitions for json-server 0.14
// Project: https://github.com/typicode/json-server
// Definitions by: Jeremy Bensimon <https://github.com/jeremyben>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
import { NextHandleFunction } from 'connect';
import { Application, RequestHandler, Router } from 'express';
/**
* Returns an Express server.
*/
export function create(): Application;
/**
* Returns middlewares used by JSON Server.
*/
export function defaults(options?: MiddlewaresOptions): RequestHandler[];
/**
* Returns JSON Server router.
* @param source Either a path to a json file (e.g. `'db.json'`) or an object in memory
* @param options Set foreign key suffix (default: `'Id'`)
*/
export function router(source: string | object, options?: { foreignKeySuffix: string }): Router;
/**
* Add custom rewrite rules.
*/
export function rewriter(rules: { [rule: string]: string }): Router;
/**
* Returns body-parser middleware used by JSON Server router.
*
* @returns
* ```
* [bodyParser.json({ limit: '10mb', extended: false }), bodyParser.urlencoded({ extended: false })]
* ```
*/
export const bodyParser: [NextHandleFunction, NextHandleFunction];
export interface MiddlewaresOptions {
/**
* Path to static files
* @default "public" (if folder exists)
*/
static?: string;
/**
* Enable logger middleware
* @default true
*/
logger?: boolean;
/**
* Enable body-parser middleware
* @default true
*/
bodyParser?: boolean;
/**
* Disable CORS
* @default false
*/
noCors?: boolean;
/**
* Accept only GET requests
* @default false
*/
readOnly?: boolean;
}

View File

@@ -0,0 +1,41 @@
import * as jsonServer from 'json-server';
const server = jsonServer.create();
const inMemoryDbRouter = jsonServer.router({ todos: [] as any[], users: [] as any[] });
const router = jsonServer.router('db.json', { foreignKeySuffix: '_id' });
const middlewaresOptions: jsonServer.MiddlewaresOptions = {
bodyParser: true,
logger: false,
noCors: true,
readOnly: true,
static: 'assets',
};
const middlewares = jsonServer.defaults(middlewaresOptions);
const rewriter = jsonServer.rewriter({
'/api/*': '/$1',
'/blog/:resource/:id/show': '/:resource/:id',
});
server.use(jsonServer.bodyParser);
server.use((req, res, next) => {
if (req.method === 'POST') {
req.body.createdAt = Date.now();
}
next();
});
server.use(rewriter);
server.use(middlewares);
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running');
});

View File

@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "json-server-tests.ts"]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }