4.0.0 • Published 6 months ago

bla v4.0.0

Weekly downloads
1,761
License
MIT
Repository
github
Last release
6 months ago

bla

NPM version Build Status

Installation

npm i bla

Also you need to install runtypes to define params schema.

npm i runtypes

Quick start

Server side

Write API method declaration

import { ApiMethod } from 'bla/server';
import * as runtypes from 'runtypes';

const helloMethod = new ApiMethod({
    params: runtypes.Record({
        name: runtypes.String
    }),
    
    action: params => `Hello, ${params.name}`;
});

export default helloMethod;

Save it to api/hello.ts.

Create api with declared methods

import { ApiMethod } from 'bla/server';
import helloMethod from './api/hello.ts';

const api = new Api({
    hello: helloMethod
});

// Export api contract to use it on client side
type ApiContract = ExtractApiContract<typeof api>;

export default api;
export { ApiContract };

Save it to api.ts.

Expose api as express middleware

import * as express from 'express';
import { apiMiddleware } from 'bla/server';
import api from './api';

express()
    .use('/api', apiMiddleware({ api }))
    .listen(8080);

Client side

import { Api } from 'bla/client';
import { ApiContract } from 'pathToServer/Api.ts';

const api = new Api<ApiContract>({ url: '/api' });

api.exec('hello', { name: 'Stepan' }).then(res => {
    console.log(res); // 'Hello, Stepan'
});

FAQ

How to define both required and optional params?

Use runtypes.Intersect:

const getObject = new ApiMethod({
    params: runtypes.Intersect(
        runtypes.Record({
            id: runtypes.String
        }),
        runtypes.Partial({
            uid: runtypes.String
        })
    }),
    
    action: params => {
        // typeof `params.id` is `string`
        // typeof `params.uid` is `string | undefined`
    }
});
4.0.0

6 months ago

3.7.0

9 months ago

3.6.0

2 years ago

3.5.2

3 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.0

3 years ago

3.3.0-rc.0

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.2.0

4 years ago

2.1.0

5 years ago

2.0.0

5 years ago

2.0.0-rc.30

5 years ago

2.0.0-rc.28

5 years ago

2.0.0-rc.29

5 years ago

2.0.0-rc.27

5 years ago

2.0.0-rc.26

5 years ago

2.0.0-rc.25

5 years ago

2.0.0-rc.24

5 years ago

2.0.0-rc.23

5 years ago

2.0.0-rc.22

5 years ago

2.0.0-rc.20

5 years ago

2.0.0-rc.21

5 years ago

2.0.0-rc.19

5 years ago

2.0.0-rc.18

5 years ago

2.0.0-rc.17

5 years ago

2.0.0-rc.16

5 years ago

2.0.0-rc.15

5 years ago

2.0.0-rc.14

5 years ago

2.0.0-rc.13

6 years ago

2.0.0-rc.12

6 years ago

2.0.0-rc.11

6 years ago

2.0.0-rc.10

6 years ago

2.0.0-rc.8

6 years ago

2.0.0-rc.9

6 years ago

2.0.0-rc.7

6 years ago

2.0.0-rc.5

6 years ago

2.0.0-rc.6

6 years ago

2.0.0-rc.4

6 years ago

2.0.0-rc.2

6 years ago

2.0.0-rc.3

6 years ago

2.0.0-rc.1

6 years ago

2.0.0-rc.0

6 years ago

1.9.2

6 years ago

1.9.1

6 years ago

1.9.0

8 years ago

1.9.0-dev

9 years ago

1.8.0

9 years ago

1.7.0

9 years ago

1.6.1

9 years ago

1.6.0

9 years ago

1.5.2

10 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.3

10 years ago

1.4.2

10 years ago

1.4.1

10 years ago

1.4.0

10 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.0

11 years ago

1.0.0

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.15

11 years ago

0.0.14

11 years ago

0.0.13

11 years ago

0.0.12

11 years ago

0.0.11

11 years ago

0.0.0

12 years ago