3.6.0 • Published 9 months ago

bla v3.6.0

Weekly downloads
1,761
License
MIT
Repository
github
Last release
9 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`
    }
});
3.6.0

9 months ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.0

2 years ago

3.3.0-rc.0

2 years ago

3.3.0

2 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.2.0

3 years ago

2.1.0

4 years ago

2.0.0

4 years ago

2.0.0-rc.30

4 years ago

2.0.0-rc.28

4 years ago

2.0.0-rc.29

4 years ago

2.0.0-rc.27

4 years ago

2.0.0-rc.26

4 years ago

2.0.0-rc.25

4 years ago

2.0.0-rc.24

4 years ago

2.0.0-rc.23

4 years ago

2.0.0-rc.22

4 years ago

2.0.0-rc.20

4 years ago

2.0.0-rc.21

4 years ago

2.0.0-rc.19

4 years ago

2.0.0-rc.18

4 years ago

2.0.0-rc.17

4 years ago

2.0.0-rc.16

4 years ago

2.0.0-rc.15

4 years ago

2.0.0-rc.14

4 years ago

2.0.0-rc.13

4 years ago

2.0.0-rc.12

4 years ago

2.0.0-rc.11

4 years ago

2.0.0-rc.10

4 years ago

2.0.0-rc.8

4 years ago

2.0.0-rc.9

4 years ago

2.0.0-rc.7

4 years ago

2.0.0-rc.5

4 years ago

2.0.0-rc.6

4 years ago

2.0.0-rc.4

4 years ago

2.0.0-rc.2

4 years ago

2.0.0-rc.3

4 years ago

2.0.0-rc.1

4 years ago

2.0.0-rc.0

4 years ago

1.9.2

4 years ago

1.9.1

5 years ago

1.9.0

7 years ago

1.9.0-dev

8 years ago

1.8.0

8 years ago

1.7.0

8 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.2

9 years ago

1.5.1

9 years ago

1.5.0

9 years ago

1.4.3

9 years ago

1.4.2

9 years ago

1.4.1

9 years ago

1.4.0

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.1.1

9 years ago

0.1.0

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.0

11 years ago