# ts-response-schema

> Type check API responses using https://github.com/jannes-io/ts-object-schema

Latest version **0.1.1** (published 2019-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-response-schema
pnpm add ts-response-schema
yarn add ts-response-schema
bun add ts-response-schema
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2019-11-04 |
| First published | 2019-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jannes Drijkoningen |
| Maintainers | jannes-d |
| Keywords | type, typescript, api, response, schema |

## Links

- npm: https://www.npmjs.com/package/ts-response-schema
- Repository: https://github.com/jannes-io/ts-response-schema
- Homepage: https://github.com/jannes-io/ts-response-schema#readme
- Issues: https://github.com/jannes-io/ts-response-schema/issues
- npm.io page: https://npm.io/package/ts-response-schema

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2019-11-04
- 0.1.0 — 2019-11-04

## README

# TS Response Schema

Combines [TS Object Schema](https://www.npmjs.com/package/ts-shallow-object-schema)
and [apisauce](https://www.npmjs.com/package/apisauce) to create the ultimate type-safe api experience.

![License](https://img.shields.io/github/license/jannes-io/ts-response-schema)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue)](https://www.typescriptlang.org/) 

## Usage

```typescript
import { Schema } from 'ts-shallow-object-schema';
import { createApi } from 'apisauce';
import { curryValidator, validateResponse } from '../';

interface ITest {
  testString: string;
  testInt: number;
  testObj: object;
  testArr: number[];
  testOptional?: number;
}

const testSchema: Schema<ITest> = [{
  key: 'testString',
  type: 'string',
}, {
  key: 'testInt',
  type: 'number',
}, {
  key: 'testObj',
  type: 'object',
}, {
  key: 'testArr',
  type: 'array',
}, {
  key: 'testOptional',
  type: 'number',
  optional: true,
}];

const api = createApi({
  baseURL: 'https://example.com/',
});

const testSchemaValidator = curryValidator(testSchema);

// Example of a valid response:
const res = validateResponse(testSchemaValidator, api.get(''));
if (res.ok) {
  // Strict types now ensure that `res.data` is of type `ITest`
  // So we can safely use it as such
  console.log(res.data.testString);
}

// Example of an invalid response
const res2 = validateResponse(testSchemaValidator, api.get(''));
if (!res2.ok && res2.data === 'Validation failed') {
  // Strict types now ensure that the API did return a response.
  // But the response was not a valid `ITest`
  console.error('Invalid response', res2);
}
```

---
_Source: https://npm.io/package/ts-response-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
