
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
@vicinity/typepoint
Advanced tools
Library for defining, serving and consuming strongly typed endpoints in TypeScript
Library for easily defining, enforcing, consuming, and/or serving strongly typed RESTful API endpoints in TypeScript.
npm install @typepoint/core --save
TypePoint requires at least TypeScript v2.8.
npm install typescript --save-dev
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);
});
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! 😸
FAQs
Library for defining, serving and consuming strongly typed endpoints in TypeScript
We found that @vicinity/typepoint demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.