Skip to content

About

Type-safe REST API in JavaScript

Resources

Stars

9 stars

Watchers

1 watching

Forks

Repository files navigation

new-request

Type-safe wrappers for third-party REST APIs.

Supports Twilio, Postmark, and more. Show all

// before - nothing is typed
await fetch('https://api.postmarkapp.com/email', {
	method: 'POST',
	headers: {
		'Accept': 'application/json',
		'Content-Type': 'application/json',
		'X-Postmark-Server-Token': 'secret',
	},
	body: JSON.stringify(body),
});

// after - fully typed request and response
await sendEmail(body, { serverToken: 'secret', from: '[email protected]' });

Features

  • Type Safety: Fully typed request and response bodies
  • Zero Overhead: Primarily types with minimal runtime code
  • Easy Migration: Uses each API's native request body format
  • Explicit Errors: Type-safe responses without try...catch
  • Fetch API: Built on the Fetch API with override support

Installation

npm install new-request

Usage

All modules follow a consistent pattern:

const response = await moduleName(requestBody, options);

// Failures are returned, not thrown
if (response instanceof Error) return; // network error, etc.

// Response types are automatically narrowed
if (response.ok) response; // success type
if (!response.ok) response; // error type

Example

Sending an email with the Postmark API:

import { sendEmail, type Options } from 'new-request/email/postmark/POST';

// Options can be modularized and exported
const options: Options = {
	serverToken: 'your_server_token_here',
	from: '[email protected]',
};

const response = await sendEmail(
	{
		// Request body matches the Postmark API for easy migration
		// https://postmarkapp.com/developer/api/email-api
		To: '[email protected]',
		Subject: 'Hello World',
		TextBody: 'Email body',
		From: '[email protected]', // optional override
	},
	options,
);

if (response instanceof Error) {
	// Network error, unparsable body, etc.
	console.error('Request failed:', response.message);
	return;
}

if (!response.ok) {
	response.status; // 401 | 404 | 413 | 415 | 422 | 429 | 500 | 503
	response.body.ErrorCode; // Postmark error code
	return;
}

response.body.MessageID; // 200 OK

Services

Note

나이스 교육정보 개방 포털 학교기본정보 was removed. Use neis-school-info instead.

Email

import { sendEmail } from 'new-request/email/postmark/POST';

Message (Web Hook, Push, etc.)

import { sendMessage } from 'new-request/message/bizgo/v1/send/omni/POST';
import { sendMessage } from 'new-request/message/dooray/POST';
import { pushMessage } from 'new-request/message/pushover/1/POST';

SMS

import { sendSms } from 'new-request/sms/nhn/v3.0/POST';
import { sendSms } from 'new-request/sms/twilio/2010-04-01/POST';

TTS

import { textToSpeech } from 'new-request/tts/naver/v1/POST';

About

Type-safe REST API in JavaScript

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages