npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@typepoint/core

v0.11.0

Published

Library for defining, serving and consuming strongly typed endpoints in TypeScript

Downloads

31

Readme

CircleCI

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! 😸