@gruzf/request
Version:
An easy way to create an http request for NextJS and more
287 lines (200 loc) • 6.01 kB
Markdown
[](https://npmjs.org/package/@gruzf/numen)
[](https://www.npmjs.com/org/gruzf)
This library is designed to create HTTP requests.
> GRUZF Request is written in `typescript` and support the [NextJS](https://nextjs.org/) library. Thanks to middleware, you can bypass **cors**.
- [Install](
- [Usage](
- [DataSourse](
- [RequestClass API](
- [baseURL](
- [headers](
- [defaultData](
- [defaultQueryParams](
- [pathOptions](
- [bodyEncoder](
- [...other](
- [RequestGroupClass API](
- [prefix](
- [pathOptions](
- [Create api route (optional)](#create-api-route-optional)
- [Create request](#create-request)
## Install
```bash
npm install @gruzf/request
```
or
```bash
yarn add @gruzf/request
```
## Usage
### DataSourse
```ts
import { Method, Request, RequestGroup } from "@gruzf/request/RequestClass";
type Response = {
success: boolean;
};
class City extends RequestGroup {
constructor() {
super({
prefix: "city",
});
}
update: Method<Response, { custom: string }> = async ({ put }) => {
return await put("/update");
};
}
const groups = { city: new City() };
export class MyAPI extends Request<typeof groups> {
constructor() {
super({
baseURL: "https://myapi.com",
groups,
});
}
getUsers: Method<Response> = async ({ get }) => {
return await get("/get-user");
};
getUser: Method<Response, { id: number }> = async ({ get }) => {
return await get("/get-user/:id");
};
// the variables will be automatically passed to the query,
// but you can still get them
addUser: Method<Response, { name: string }> = async ({ post }, variables) => {
return await post("/add-user");
};
}
export default MyAPI;
```
- Type: `string`
- Default: `undefined`
Request URL
- Type: `object`
- Default: `undefined`
Request headers
- Type: `Record<string, any>`
- Default: `undefined`
Initial data (body or query params) that is added to each request
##### defaultQueryParams
- Type: `Record<string, any>`
- Default: `undefined`
Initial query params that is added to each request
##### pathOptions
- Type: `(request?: NextApiRequest | NextRequest | IncomingMessage) => Record<string, string | number | boolean | null | undefined>`
- Default: `undefined`
Parameters for generating the url path
```ts
/**
*
* pathOptions: () => {
* return {
* id: 1009
* }
* }
*
*/
async updateUser({ put }) {
return await put("/:id/update"); // -> https://myapi.com/1009/update
}
```
- Type: `(object: Record<string, any>) => BodyInit`
- Default: `undefined`
Request body encoder
```ts
import qs from "qs";
export class MyAPI extends Request<typeof groups> {
constructor() {
super({
...,
bodyEncoder: qs.stringify,
headers: {
"content-type": "application/x-www-form-urlencoded",
},
...
});
}
addUser: Method<Response, { name: string }> = async ({ post }) => {
return await post("/add-user");
};
}
```
All other ky options
- Type: `string`
- Default: `undefined`
You can specify a prefix that will be inserted at the beginning of each request of this group
```ts
// without prefix "city"
update: Method<Response> = async ({ put }) => {
return await put("/update"); // -> https://myapi.com/update
};
// with prefix "city"
update: Method<Response> = async ({ put }) => {
return await put("/update"); // -> https://myapi.com/city/update
};
```
- Type: `(request?: NextApiRequest | NextRequest | IncomingMessage) => Record<string, string | number | boolean | null | undefined>`
- Default: `undefined`
Parameters for generating the url path
```ts
/**
*
* pathOptions: () => {
* return {
* id: 1009
* }
* }
*
*/
async updateUser({ put }) {
return await put("/:id/update"); // -> https://myapi.com/1009/update
}
```
If you are using [NextJS](https://nextjs.org/) it is recommended to use middleware.
Create new file `app/api/request/route.js`. You can change the page name by `REQUEST_PAGE` environment variable.
```ts
import apiRoute from "@gruzf/request/next/apiRoute";
import { methods } from "@methods";
type Methods = typeof methods;
type MethodsType = {
[]: Methods[K];
};
export const { POST } = apiRoute(methods);
declare module "@gruzf/request" {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface QueryOverrides extends MethodsType {}
}
```
```js
import request from "@gruzf/request";
const users = await request("myApi")("getUser");
const response = await request("myApi")("addUser", { name: "Jhon", age: 23 });
// if you used groups, then the format looks like "group:method"
const cityResponse = await request("myApi")("city:update");
```
```js
import setup from "@gruzf/request/direct";
import MyAPI from "./datasourse/MyAPI";
const myApi = new MyAPI();
const directRequest = setup({ myApi });
const users = await directRequest("myApi")("getUser");
const response = await directRequest("myApi")("addUser", {
name: "Jhon",
age: 23,
});
// if you used groups, then the format looks like "group:method"
const cityResponse = await directRequest("myApi")("city:update");
```