0.20.0 • Published 6 months ago

@rest-vir/define-service v0.20.0

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
6 months ago

@rest-vir/define-service

Part of the rest-vir suite. This package is used for defining a declarative and type safe REST service with both endpoints and WebSockets.

See the full docs at https://electrovir.github.io/rest-vir

Installation

npm i @rest-vir/define-service

Usage

Define your service:

import {AnyOrigin, defineService, HttpMethod} from '@rest-vir/define-service';

export const myService = defineService({
    /** The name of your service. This will be visible to all consumers of this service definition. */
    serviceName: 'my-service',
    /**
     * The origin at which the service will be hosted. Fetch requests and WebSocket connections will
     * be sent to this service will be sent to this origin.
     *
     * It is recommended to use a ternary to switch between dev and prod origins.
     */
    serviceOrigin: isDev ? 'http://localhost:3000' : 'https://example.com',
    /**
     * The service's `origin` requirement for all endpoint requests and WebSocket connections. This
     * is used for CORS handshakes.
     *
     * This can be a string, a RegExp, a function, or an array of any of those. (If this is an
     * array, the first matching array element will be used.)
     *
     * Set this to `AnyOrigin` (imported from `'@rest-vir/define-service'`) to allow any origins.
     * Make sure that you're okay with the security impact this may have on your users of doing so.
     */
    requiredClientOrigin: AnyOrigin,
    endpoints: {
        '/my-endpoint': {
            /** This endpoint requires all requests to contain a string body. */
            requestDataShape: '',
            /** This endpoint's response body will always be empty. */
            responseDataShape: undefined,

            methods: {
                [HttpMethod.Post]: true,
            },
        },
        /** Express-style path params are allowed. */
        '/my-endpoint/:user-id': {
            /** This endpoint expects no request body data. */
            requestDataShape: undefined,
            /**
             * This endpoint will always response with data that matches:
             *
             *     {
             *         username: string,
             *         firstName: string,
             *         lastName: string
             *     }
             */
            responseDataShape: {
                username: '',
                firstName: '',
                lastName: '',
            },
            methods: {
                [HttpMethod.Get]: true,
            },
            /** Each endpoint may override the service's origin requirement. */
            requiredClientOrigin: 'https://example.com',
        },
    },
    webSockets: {
        '/my-web-socket': {
            /** This WebSocket requires all messages from the client to be a string. */
            messageFromClientShape: '',
            /** Same for messages from the host. */
            messageFromHostShape: '',
        },
    },
});
0.20.0

6 months ago

0.19.2

6 months ago

0.19.1

6 months ago

0.19.0

7 months ago

0.18.1

7 months ago

0.18.0

7 months ago

0.17.2

7 months ago

0.17.1

7 months ago

0.17.0

7 months ago

0.16.0

7 months ago

0.15.1

7 months ago

0.15.0

7 months ago

0.14.0

7 months ago

0.13.0

8 months ago

0.12.0

8 months ago

0.11.4

8 months ago

0.11.3

8 months ago

0.11.2

8 months ago

0.11.1

8 months ago

0.11.0

8 months ago

0.10.0

8 months ago

0.9.1

8 months ago

0.9.0

9 months ago

0.8.3

9 months ago

0.8.2

9 months ago

0.8.1

9 months ago

0.8.0

9 months ago

0.7.0

9 months ago

0.6.0

9 months ago

0.5.4

9 months ago

0.5.3

9 months ago

0.5.2

9 months ago

0.5.1

9 months ago

0.5.0

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.4

9 months ago

0.3.3

9 months ago

0.3.2

9 months ago

0.3.1

9 months ago

0.3.0

9 months ago

0.2.0

9 months ago

0.1.0

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

10 months ago

0.0.2

10 months ago