Skip to main content

Functions

httpCall()

httpCall<T>(endpoint, resource, method, payload?, headers?): Promise<T>

Defined in: utils/http-call.ts:46

Makes an HTTP request to an API endpoint.

Useful in E2E and integration tests for triggering API endpoints and verifying responses. Includes a 10-second timeout by default.

Type Parameters

T

T

The expected response type

Parameters

endpoint

string

The base URL of the API (e.g., 'https://api.example.com')

resource

string

The resource path (e.g., '/users/123')

method

string

The HTTP method (GET, POST, PUT, DELETE, etc.)

payload?

Record<string, string | number | boolean | object>

Optional request body for POST/PUT requests

headers?

Record<string, string>

Optional additional headers to include

Returns

Promise<T>

The parsed JSON response

Throws

Error if the request fails or returns a non-2xx status

Examples

// Trigger an order creation endpoint
const order = await httpCall<Order>(
'https://api.example.com',
'/orders',
'POST',
{ productId: 'PROD-123', quantity: 2 },
{ Authorization: `Bearer ${token}` }
);

// Then verify the order was persisted
const { items } = await query<Order>({
tableName: 'orders-table',
keyConditionExpression: 'pk = :pk',
expressionAttributeValues: { ':pk': `ORDER#${order.id}` }
});
expect(items[0].status).toBe('PENDING');
// Simple GET request
const user = await httpCall<User>(
'https://api.example.com',
'/users/123',
'GET'
);