@schornio/express-api-interface
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

@schornio/express-api-interface

Build Status

Router

import { HTTPNotFoundError, Router } from '@schornio/express-api-interface';

export const router = new Router();

router.get('/endpoint_sync', (request) => {
  const q = request.ensureQuery('q');
  const result = searchSync(q);
  return result;
});

router.get('/endpoint_async', async (request) => {
  const q = request.ensureQuery('q');
  const result = await searchAsync(q);
  return result;
});

router.get('/endpoint/:param1', (request) => {
  const q = request.ensureParam('param1');
  const result = searchSync(q);
  return result;
});

router.post('/endpoint', (request) => {
  const body = request.ensureBody(assertBodyFN);
  saveBody(body);
});

router.post('/redirect', (request) => {
  return new Redirect(Redirect.TemporaryRedirect, '/endpoint');
});

router.get('/error', async (request) => {
  throw new HTTPNotFoundError('Gets converted to a 404 response');
});

App

import { App } from '@schornio/express-api-interface';

const PORT = process.env.PORT || '80';

const app = new App();

app.addSetting('trust proxy', true);

app.registerRoutes([['/endpoint', router]]);

/* eslint-disable-next-line no-console */
app.start(PORT).then(() => console.log(`Listening on port ${PORT}...`));

Request

  • ensureBody<T>(assert: AssertBody<T>): T
  • ensureParam(name: string): string
  • ensureQuery(name: string): string
  • setPayload<T>(name: string, payload: T): void
  • getPayload<T>(name: string): T | undefined
  • ensurePayload<T>(name: string): T
  • getRequestCore(): ExpressRequest

Static

  • setPayload<T>(expressRequest: ExpressRequest, name: string, payload: T): void
  • getPayload<T>(expressRequest: ExpressRequest, name: string): T | undefined
  • ensurePayload<T>(expressRequest: ExpressRequest, name: string): T

Readme

Keywords

none

Package Sidebar

Install

npm i @schornio/express-api-interface

Weekly Downloads

1

Version

1.5.0

License

ISC

Unpacked Size

33.6 kB

Total Files

44

Last publish

Collaborators

  • schornio