Skip to content

TypeScript

Getting started

1. Install the dependency

Add the typedrest NPM package to your project:

npm install typedrest --save

2. Create an entry endpoint

Create an EntryEndpoint pointing at your API:

import { EntryEndpoint } from 'typedrest/endpoints';

const client = new EntryEndpoint(new URL("https://example.com/api/"));

3. Define your client class

Extend EntryEndpoint and expose your API's resources as properties:

import { EntryEndpoint } from 'typedrest/endpoints';
import { CollectionEndpoint } from 'typedrest/endpoints/generic';

class MyClient extends EntryEndpoint {
    get contacts() {
        return new CollectionEndpoint<Contact>(this, "./contacts");
    }
}

4. Use the client

Use your client to interact with the API:

const client = new MyClient(new URL("https://example.com/api/"));

// Read all contacts
const contacts: Contact[] = await client.contacts.readAll();

// Create a new contact
const newContact = await client.contacts.create({name: "Smith"});

// Read a specific contact
const contact: Contact = await newContact.read();

5. Next steps

See also