Skip to main content
Module

x/911/src/json.ts>json

Opinionated collection of helper tools. Assembled with Deno in mind.
Go to Latest
function json
import { json } from "https://deno.land/x/911@0.1.1/src/json.ts";

Create a new response in JSON format, with built-in error handling and sensible defaults for the response headers. Accepts an optional second parameter to configure the response output. It is typed as a union of ResponseInit and JSON.stringify params 2 & 3.

Examples

Example 1

import { json } from "https://deno.land/x/911@0.1.1/mod.ts";
import { STATUS_TEXT } from "https://deno.land/std/http/http_status.ts";

const response = json({
 message: "Hello World",
});

// Equivalent to:
// new Response("{\"message\":\"Hello World\"}", {
//   status: 200,
//   headers: {
//     "content-type": "application/json; charset=utf-8",
//     "content-length": "17",
//     "etag": "W/5c8qKJoE6P9Q",
//     "access-control-allow-origin": "*",
//   },
// });

Type Parameters

T extends any

Parameters

data: T

the response body to be stringified and sent

optional
unnamed 1: Partial<JSONInit>

optional configurations for Response and JSON

Type Parameters

T extends any

Parameters

data: T
optional
unnamed 1: Partial<JSONInitAlt>