0.20.0 • Published 7 months ago

@rest-vir/define-service v0.20.0

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
7 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

7 months ago

0.19.2

7 months ago

0.19.1

7 months ago

0.19.0

7 months ago

0.18.1

8 months ago

0.18.0

8 months ago

0.17.2

8 months ago

0.17.1

8 months ago

0.17.0

8 months ago

0.16.0

8 months ago

0.15.1

8 months ago

0.15.0

8 months ago

0.14.0

8 months ago

0.13.0

8 months ago

0.12.0

8 months ago

0.11.4

9 months ago

0.11.3

9 months ago

0.11.2

9 months ago

0.11.1

9 months ago

0.11.0

9 months ago

0.10.0

9 months ago

0.9.1

9 months ago

0.9.0

9 months ago

0.8.3

10 months ago

0.8.2

10 months ago

0.8.1

10 months ago

0.8.0

10 months ago

0.7.0

10 months ago

0.6.0

10 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.5.0

10 months ago

0.4.1

10 months ago

0.4.0

10 months ago

0.3.4

10 months ago

0.3.3

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

0.1.0

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago