UNPKG

@gati-framework/runtime

Version:

Gati runtime execution engine for running handler-based applications

52 lines 1.46 kB
/** * @module runtime/request * @description Request object factory for Gati framework */ import { parse as parseUrl } from 'url'; /** * Create a Request object from RequestOptions * * @param options - Request creation options * @returns Request object * * @example * ```typescript * const req = createRequest({ * method: 'GET', * path: '/api/users/123', * params: { id: '123' }, * raw: incomingMessage * }); * ``` */ export function createRequest(options) { // Parse query string if not provided let query = options.query ?? {}; let path = options.path; if (!options.query && options.raw.url) { const parsed = parseUrl(options.raw.url, true); // Extract query parameters const parsedQuery = parsed.query; query = Object.entries(parsedQuery).reduce((acc, [key, value]) => { if (value !== undefined) { acc[key] = value; } return acc; }, {}); // Use pathname as path (without query string) path = parsed.pathname || path; } // Extract headers from IncomingMessage if not provided const headers = options.headers ?? options.raw.headers; return { method: options.method, path, query, params: options.params ?? {}, headers, body: options.body, rawBody: options.rawBody, raw: options.raw, }; } //# sourceMappingURL=request.js.map