@retrohacker/json-types
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@retrohacker/json-types

Types to help dealing with unstructured JSON payloads.

Types

export type JsonPrimitive = string | number | boolean | null
export type JsonValue = JsonPrimitive | JsonObject | JsonArray
export type JsonObject = { [key: string]: JsonValue }
export type JsonArray = JsonValue[]

Usage

import { JsonObject } from 'json-types'

type User = {
  id: number,
  name: string,
  email: string
}
const { statusCode, body } = await request(
  `http://localhost:3000/user/42`
);

if (statusCode !== 200) {
  return null;
}

const remoteUser = (await body.json()) as JsonObject;
const localUser:User = {
  id: 42
};

localUser.name = [
  remoteUser.firstName,
  remoteUser.middleName,
  remoteUser.lastName,
]
  .filter((s) => s)
  .join(" ");

if (typeof remoteUser.emailAddress === "string") {
  localUser.email = remoteUser.emailAddress;
}

Dependents (0)

Package Sidebar

Install

npm i @retrohacker/json-types

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

2.23 kB

Total Files

5

Last publish

Collaborators

  • retrohack3r