@vicinity/typepoint
v0.11.0
Published
Library for defining, serving and consuming strongly typed endpoints in TypeScript
Downloads
12
Readme
Install
npm install @typepoint/core --save
TypePoint requires at least TypeScript v2.8.
npm install typescript --save-dev
Usage
Let's say you need an endpoint which returns a Todo.
shared/models/todo.d.ts
interface Todo {
id: string;
title: string;
isCompleted: boolean;
}
First you define your endpoint in a shared place
shared/endpoints/todos/get.ts
import { Empty, EndpointDefinition } from '@typepoint/core/shared';
// Define this endpoint's request params, request body, and response body as well as the path
export const GetTodo = new EndpointDefinition<{ id: string }, Empty, Todo>(
path => path.literal('todos').param('id')
);
Now you've defined your endpoint, lets define a handler for it
server/handlers/todos/get.ts
import { defineHandler } from '@typepoint/core/server';
import { GetTodo } from '../shared/endpoints/todos/get.ts';
import { todoService } from './todoService';
export const GetTodoHandler = defineHandler(GetTodo, context => {
const id = context.request.params.id;
const todo = await todoService.get(id);
if (todo) {
context.response.body = todo;
} else {
context.response.statusCode = 404;
}
});
Now we just need to tie it all together by exporting our router as middleware that our express app can use.
server/app.ts
import * as express from 'express';
import { Router } from '@typepoint/core/server';
import { toMiddleware } from '@typepoint/core/server/express';
import { GetTodoHandler } from './handlers/todos/get.ts';
const app = express();
const router = new Router({
handlers: [
GetTodoHandler
]
});
// Convert router to middleware that express can use
app.use(toMiddleware(router));
const port = 3000;
app.listen(3000, () => {
console.log(`Listening on port ${ port }`);
})
Then on the client side you can call your endpoint like so
client/app.ts
import { TypePointClient } from '@typepoint/core/client';
import { GetTodo } from '../shared/endpoints/todos/get';
const client = new TypePointClient({
// location of your endpoints
server: 'https://www.example.com/api'
});
// The client will fetch https://www.example.com/api/todos/123
client.fetch(GetTodo, {
params: { // Params will be required and strongly typed
id: '123'
}
}).then(response => {
const todo = response.body; // body will be strongly typed to a Todo
alert(todo.title);
});
Note about breaking changes
Currently TypePoint is pre v1.0.0, thus breaking changes may be introduced in minor versions instead of major versions. Patch versions will continue to just include bug fixes and other non breaking changes. Once TypePoint has reached 1.0.0 it will follow strict semantic versioning.
Got an problem or suggestion? Submit an issue!
Want to contribute? Fork the repository and submit a pull request! 😸