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

ameliance-fetch

v0.0.3

Published

Library for data fetching

Downloads

9

Readme

aFetch - a small wrapper to simplify work with fetch

Installing

npm i ameliance-fetch

Usage

1. Create common fetch

// api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

const aFetchApi = aFetch(API_URL);

aFetch also accepts additional options →

If necessary, here you can globally specify the types of returned error body data that can be returned with an error for a given function instance:

// src/api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

type ErrorCode = "USERNAME_NOT_FOUND" | "INVALID_EMAIL" | "DATABASE_ERROR" | "TOKEN_HAS_EXPIRED";
type ResponseErrorBody = { code: ErrorCode; username: string } | { code: ErrorCode; email: string };

const aFetchApi = aFetch<ResponseErrorBody>(API_URL);

Default types:

type AFetchErrorBody = unknown;

2. Use aFetch to create api

Get request:

// src/api/rest/todo/get-todo.ts
import { aFetchApi } from "../a-fetch-api";

async function getTodo(page: number) {
	return await aFetchApi.get(`/todos/${page}`);
}

Post request:

// src/api/rest/todo/add-todo.ts
import { aFetchApi } from "../a-fetch-api";

type TodoRequest = {
	userId: string;
	title: string;
	text: string;
};

async function addTodo(body: TodoRequest) {
	return await aFetchApi.post(`/todos`, body);
}

If necessary, here you can specify the types of body returned data by fetch and / or data expected by the API body, as well as the types of error body data that may be returned with an error:

Get request:

// src/api/rest/todo/get-todo.ts
import { aFetchApi } from "../a-fetch-api";

type TodoResponse = {
	id: number;
	userId: number;
	title: string;
	text: string;
	completed: boolean;
};

type ErrorCode = "USERNAME_NOT_FOUND" | "INVALID_EMAIL" | "DATABASE_ERROR" | "TOKEN_HAS_EXPIRED";
type ResponseErrorBody = { code: ErrorCode; username: string } | { code: ErrorCode; email: string };

async function getTodo(page: number) {
	return await aFetchApi.get<TodoResponse, ResponseErrorBody>(`/todos/${page}`);
}

Post request:

// src/api/rest/todo/add-todo.ts
import { aFetchApi } from "../a-fetch-api";

type TodoRequest = {
	userId: string;
	title: string;
	text: string;
};

type TodoResponse = {
	id: number;
	userId: number;
	title: string;
	text: string;
	completed: boolean;
};

type ErrorCode = "USERNAME_NOT_FOUND" | "INVALID_EMAIL" | "DATABASE_ERROR" | "TOKEN_HAS_EXPIRED";
type ResponseErrorBody = { code: ErrorCode; username: string } | { code: ErrorCode; email: string };

async function addTodo(body: TodoRequest) {
	return await aFetchApi.post<TodoRequest, TodoResponse, ResponseErrorBody>(`/todos`, body);
}

Default types:

type AFetchRequest = unknown;
type AFetchBody = unknown;
type AFetchErrorBody = unknown;

3. Processing of requests

// src/app.ts
async function app() {
	const resp = await getTodo(1);
	if (resp.ok) {
		console.log("resp: ", resp);
		// resp: {
		// 	ok: true,
		// 	data: {
		// 		id: 1,
		// 		userId: 1,
		// 		title: "aFetch",
		// 		test: "Make a library",
		// 		completed: false,
		// 	},
		// };
	} else {
		console.log("resp: ", resp);
		// resp: {
		// 	ok: false,
		// 	status: 404,
		// 	message: 'Can't find username in database',
		// 	data: {
		// 		code: 'USERNAME_NOT_FOUND',
		// 		username: 'amelianceskymusic'
		// 	},
		// };
	}
}

app();

4. Options

The aFetch function takes the second parameter - options:

headersInit: HeadersInit;
showConsoleError: boolean;
errorTitle: string;
deepError: boolean;

1. headersInit — setting common headers init

// api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

const headersInit = {
	"X-API-Host": "api.todo.com",
	"X-API-Key": process.env.TODO_API_KEY,
};

const aFetchApi = aFetch(API_URL, { headersInit });

2. showConsoleError — displays errors in the console. If false, errors are not printed to the console

Default value: true

// api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

const aFetchApi = aFetch(API_URL, { showConsoleError: false });

3. errorTitle

Default value: 'A-FETCH ERROR'

// api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

const errorTitle = 'AMELIANCESKYMUSIC ERROR;

const aFetchApi = aFetch(API_URL, { errorTitle });
AMELIANCESKYMUSIC ERROR > 404 | Not Found
	at handleAFetchError (C:\app\ameliance-fetch\src\helpers\handle-a-fetch-error.ts:29:32)
	at C:\app\ameliance-fetch\src\common-fetch.ts:57:28
	at Generator.next (<anonymous>)
	at fulfilled (C:\app\ameliance-fetch\src\common-fetch.ts:5:58)
	at processTicksAndRejections (node:internal/process/task_queues:95:5)

4. deepError — show a deep error otherwise a short version

Default value: true

// api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

const aFetchApi = aFetch(API_URL);

error in console:

A-FETCH ERROR > 404 | Not Found
   at handleAFetchError (C:\app\ameliance-fetch\src\helpers\handle-a-fetch-error.ts:29:32)
   at C:\app\ameliance-fetch\src\common-fetch.ts:57:28
   at Generator.next (<anonymous>)
   at fulfilled (C:\app\ameliance-fetch\src\common-fetch.ts:5:58)
   at processTicksAndRejections (node:internal/process/task_queues:95:5)

deepError: false

// api/rest/a-fetch-api.ts
import { aFetch } from "ameliance-fetch";

const API_URL = "https://todo.com/api";

const aFetchApi = aFetch(API_URL, { deepError: false });

error in console:

A-FETCH ERROR > 404 | Not Found

History

0.0.3 [2024_09_11]:
   *: fix publish issues

0.0.2 [2024_09_11]:
   *: update code style

0.0.1 [2024-04-26]:
   +: init package