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

@ocubist/http-request-handler

v0.2.2

Published

For personal usage

Downloads

1

Readme

HTTP Request Handler

Description

The HTTP Request Handler is a flexible and powerful library for crafting and executing HTTP requests in TypeScript. It provides a set of customizable hooks and utilities for handling various HTTP methods, response parsing, and error handling. This library leverages the @ocubist/error-alchemy package for robust error handling and utilizes Zod schemas extensively to ensure advanced type-safety with comprehensive type inference.

Installation

To install the HTTP Request Handler, use npm or yarn:

npm install @ocubist/http-request-handler
# or
yarn add @ocubist/http-request-handler

Usage

Basic Example

import { useHttpRequestHandler } from "@ocubist/http-request-handler";
import { z } from "zod";

// Define your Zod schemas
const pathParamsSchema = z.object({
  userId: z.string(),
});

const queryParamsSchema = z.object({
  includePosts: z.boolean(),
});

const responseBodySchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string(),
});

// Initialize the HTTP request handler
const { craftGetRequest } = useHttpRequestHandler({
  baseUrl: "https://api.example.com",
});

// Craft a GET request
const getUser = craftGetRequest({
  pathParamsSchema,
  queryParamsSchema,
  responseBodySchema,
  endpointTemplate: "/users/{userId}",
});

// Use the crafted request function
const fetchUser = async (userId: string) => {
  try {
    const response = await getUser({
      pathParams: { userId },
      queryParams: { includePosts: true },
    });
    return response;
  } catch (error) {
    console.error("Error fetching user:", error);
  }
};

const userResponse = await fetchUser("123");

if (userResponse) {
  const { id, name, email } = userResponse.data;
  console.log(`Fetched user '${name}' with id '${id}' and email '${email}'`);
  // Fetched user 'USER_NAME' with id '123' and email 'EMAIL'
}

Advanced Example with POST Request

import { useHttpRequestHandler } from "@ocubist/http-request-handler";
import { z } from "zod";

// Define your Zod schemas
const pathParamsSchema = z.object({
  userId: z.string(),
});

const queryParamsSchema = z.object({});

const requestBodySchema = z.object({
  title: z.string(),
  content: z.string(),
});

const responseBodySchema = z.object({
  success: z.boolean(),
  message: z.string(),
  data: z.object({
    user: z.object({
      userId: z.string(),
      name: z.string(),
      email: z.string(),
    }),
    post: z.object({
      postId: z.string(),
      title: z.string(),
      content: z.string(),
    }),
  }),
});

// Initialize the HTTP request handler
const { craftPostRequest } = useHttpRequestHandler({
  baseUrl: "https://api.example.com",
});

// Craft a POST request
const createPost = craftPostRequest({
  pathParamsSchema,
  queryParamsSchema,
  requestBodySchema,
  responseBodySchema,
  endpointTemplate: "/users/{userId}/posts",
});

// Use the crafted request function
const submitPost = async (userId: string, title: string, content: string) => {
  try {
    const response = await createPost({
      pathParams: { userId },
      queryParams: {},
      requestBody: { title, content },
    });
    return response;
  } catch (error) {
    console.error("Error creating post:", error);
  }
};

const postResponse = await submitPost(
  "123",
  "Hello World",
  "This is my first post."
);

if (postResponse) {
  const { user, post } = postResponse.data;
  console.log(
    `Post by user '${user.name}' with id '${user.userId}' and title '${post.title}': "${post.content}"`
  );
  // Post by user 'USER_NAME' with id '123' and title 'Hello World': "This is my first post."
}

API Documentation

Docs

License

The HTTP Request Handler is licensed under the MIT License. See the LICENSE file for more information.