GithubHelp home page GithubHelp logo

api-factory's Introduction

API Factory

Description

API Factory's primary goal is to create a quick and easy way to host APIs. It's not comprehensive. It's just handles basic use cases.

Example

Below is an example copied from mock.server.ts

import { API, Server, BasicAuth } from "api-factory";
import { MockDatabase } from "./mockdatabase";

const basicAuth = new BasicAuth({ authenticate: (username, password) => username == 'me' && password == 'secret' ? { username } : null });
const User = API.Factory({ authentication: [basicAuth], }, ({ user }) => user);
const Create = API.Factory({}, ({ body }) => MockDatabase.create(body));
const List = API.Factory({}, () => MockDatabase.list());
const Read = API.Factory({}, ({ pathParameters }) => MockDatabase.read(pathParameters.id));
const Update = API.Factory({}, ({ pathParameters, body }) => MockDatabase.update(pathParameters.id, body));
const Delete = API.Factory({}, ({ pathParameters }) => MockDatabase.delete(pathParameters.id));

export const MockServer =
  Server({
        port: 80,
        cors: true,
        routes: [
            {
                id: 'create',
                method: ['PUT'],
                path: '/db',
                handler: Create
            }, {
                id: 'list',
                method: ['GET'],
                path: '/db',
                handler: List
            }, {
                id: 'read',
                method: ['GET'],
                path: '/db/:id',
                handler: Read
            }, {
                id: 'update',
                method: ['POST'],
                path: '/db/:id',
                handler: Update
            }, {
                id: 'delete',
                method: ['DELETE'],
                path: '/db/:id',
                handler: Delete
            }, {
                id: 'user',
                method: ['GET'],
                path: '/user',
                handler: User
            }, {
                id: '*',
                method: ['ALL'],
                path: '*',
                handler: API.Factory({}, ({ path }) => `Not Found: ${path}`)
            }
        ]
    });

api-factory's People

Contributors

0x6563 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.