Skip to content

Latest commit

 

History

History
126 lines (94 loc) · 2.02 KB

File metadata and controls

126 lines (94 loc) · 2.02 KB

< Back

JDER Core

A standard response builder.

Compatible with all TypeScript/JavaScript frameworks with standard Response API.

Installation

Install this package as a dependency in the project:

# npm
npm i @jderstd/core

# Yarn
yarn add @jderstd/core

# pnpm
pnpm add @jderstd/core

# Deno
deno add npm:@jderstd/core

# Bun
bun add @jderstd/core

Create a Success JSON Response

To create a JSON response without data, just use createJsonResponse function:

import { createJsonResponse } from "@jderstd/core";

const route = (): Response => {
    return createJsonResponse();
}

And the response will be shown as below:

{
    "success": true,
    "data": null,
    "errors": []
}

Create a Success JSON Response with Data

The createJsonResponse function can also be used to insert data to the response:

import { createJsonResponse } from "@jderstd/core";

const route = (): Response => {
    return createJsonResponse({
        data: "Hello, World!",
    });
}

And the response will be shown as below:

{
    "success": true,
    "data": "Hello, World!",
    "errors": []
}

Create a Failure JSON response

To create a failure JSON response, add errors field to the options:

import { createJsonResponse } from "@jderstd/core";

const route = (): Response => {
    return createJsonResponse({
        errors: [
            {
                code: "server",
            },
        ],
    });
}

And the response will be shown as below:

{
    "success": false,
    "data": null,
    "errors": [
        {
            "code": "server",
            "path": [],
            "message": null
        }
    ]
}

Create a Non-JSON response

To create a non-JSON response, use createResponse function:

import { createResponse } from "@jderstd/core";

const route = (): Response => {
    return createResponse({
        status: 404,
        headers: {
            "Content-Type": "text/plain",
        },
        body: "Not Found",
    });
}