Skip to content

Latest commit

 

History

History
117 lines (78 loc) · 2.47 KB

File metadata and controls

117 lines (78 loc) · 2.47 KB

Rewrite Types

@rewritetoday/types, an official TypeScript types for the Rewrite API.

This package provides shared resource models, request/response contracts, and route builders used by Rewrite SDKs and applications. It is designed to keep API integrations strongly typed and consistent across projects.

Installation

You can use this package with your favorite package manager

bun add @rewritetoday/types
# Or
npm install @rewritetoday/types
# Or
pnpm install @rewritetoday/types
# Or
yarn add @rewritetoday/types

Using

How we document our types

  • Prefix API* Represents general API structures (returned objects, internal models, etc.).

  • Prefix REST<HTTPMethod>* Types used in direct API requests.

    • Suffix Body → request body payload E.g.: RESTPostCreateTemplateBody

    • Suffix QueryParams → query string parameters E.g.: RESTGetListWebhooksQueryParams

    • Suffix Data → data returned by the API E.g.: RESTGetListWebhooksData

  • Prefix Webhook* Represents any data that come from a webhook.

Typing API Responses

import { REST } from '@rewritetoday/rest';
import { Routes, type RESTGetProjectData } from '@rewritetoday/types/v1';

const rest = new REST(process.env.REWRITE_API_KEY);

// Return type: Promise<RESTGetWebhooksData>
async function fetchHooks() {
	const data = await rest.get<RESTGetWebhooksData>(Routes.webhooks.list());
	
	return data
}

Typing webhook events

We are using Bun here as example, but you can use it however you want.

import { type WebhookEvent, WebhookEventType } from '@rewritetoday/types/v1';

const server = Bun.serve({
	port: 3000,
	routes: {
		async '/webhooks/rewrite'(request) {
			const event: WebhookEvent = await request.json();
			
			switch (event.type) {
				case WebhookEventType.MessageSent:
					console.log('Hey, a new message!');
					
					return Response.json({});
				case ...
			}
		},
	},
});

Using Route Builders

import { API_VERSION API_BASE_URL, Routes } from '@rewritetoday/types/v1';

// https://api.rewritetoday.com/v1/templates
const CREATE_TEMPLATE_URL = `${API_BASE_URL}/${API_VERSION}/${Routes.templates.create()}`;

Made with 🤍 by the Rewrite team.
SMS the way it should be.