ts-response-schema
v0.1.1
Published
Type check API responses using https://github.com/jannes-io/ts-object-schema
Downloads
27
Maintainers
Readme
TS Response Schema
Combines TS Object Schema and apisauce to create the ultimate type-safe api experience.
Usage
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);
}