fivem-ts

a library to speed up the development of Fivem scripts and frameworks


Project maintained by Purpose-Dev Hosted on GitHub Pages — Theme by mattgraham

fivem-ts - Documentation v0.7.5Docs


fivem-ts - Documentation v0.7.5 / Server / makeRequest

Function: makeRequest()

makeRequest<T>(url, method, data?, headers?): Promise<T>

A general-purpose function for making HTTP requests.

This function performs HTTP requests using the Fetch API. It supports various HTTP methods (GET, POST, PUT, DELETE) and allows you to include request headers and body data.

Type Parameters

T

Parameters

url: string

The URL to which the request is sent.

method: RequestMethods = 'GET'

The HTTP method to use for the request.

data?: RequestData

The request body data.

headers?: RequestHeaders = {}

Optional headers to include in the request.

Returns

Promise<T>

A promise that resolves to the response data of type T.

Throws

Throws an error if the response status is not OK or if there is an error during the fetch operation.

Example

interface User {
  id: number;
  name: string;
}

async function fetchUser() {
  try {
    const user = await makeRequest<User>('https://api.example.com/user/1');
    console.log(user);
  } catch (error) {
    console.error('Error fetching user:', error);
  }
}

Defined in

server/web/makeRequest.ts:107