TypeScript
@johnhenry/http-converter includes TypeScript type definitions in
types.d.ts.
Using with TypeScript
Section titled “Using with TypeScript”import * as http from '@johnhenry/http-converter';import type { HttpRequest, HttpResponse, HarEntry } from '@johnhenry/http-converter';
// Type-safe request creationconst request: HttpRequest = { method: 'POST', url: '/api/users', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ name: 'Test' })};
// Convert with full type inference// curl.fromRequest and har.fromRequest always return a Promiseconst curl: string = await http.curl.fromRequest(request);const har: HarEntry = await http.har.fromRequest(request);Native Request/Response support
Section titled “Native Request/Response support”The library supports both custom HTTP objects and native Web API
Request/Response objects:
// Using native Requestconst request = new Request('https://api.example.com', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ data: 'test' })});
// Convert native Request to cURLconst curl = await http.curl.fromRequest(request);
// Using native Responseconst response = new Response('{"success":true}', { status: 200, headers: { 'Content-Type': 'application/json' }});
// Convert native Response to HARconst harEntry = await http.har.fromResponse(response);Type definitions
Section titled “Type definitions”All major types are exported:
HttpRequest— standard HTTP request objectHttpResponse— standard HTTP response objectHarEntry— HAR format entryHarRequest,HarResponse— HAR sub-typesCurlOptions— options for cURL generationFetchOptions— options for fetch code generation
Module types
Section titled “Module types”Each module has typed exports:
import * as string from '@johnhenry/http-converter/string';import * as har from '@johnhenry/http-converter/har';import * as curl from '@johnhenry/http-converter/curl';import * as fetch from '@johnhenry/http-converter/fetch';
// All methods are fully typedconst parsed: HttpRequest = string.parseRequest('GET / HTTP/1.1\\r\\n\\r\\n');