import Debug from "debug" ; import { URL } from "url" ; import { ClientRequestOptions , IRequestClient } from "./client" ; const debug = Debug ( "opconnect:requests" ) ; export class RequestAdapter { private client : IRequestClient ; private readonly baseURL : URL ; private readonly token : string ; public constructor ( client , opts ) { if ( ! opts . serverURL || ! opts . token ) { throw TypeError ( "Options serverURL and token are required." ) ; } this . baseURL = new URL ( opts . serverURL ) ; this . token = opts . token ; this . client = client ; } param method param path param { RequestOptions } opts returns { Promise < Response > } public async sendRequest ( method , path , opts ? ) { opts = opts || { } ; const clientOptions = { ... opts , ... { authToken : this . token } , } ; debug ( "Sending request - %s %s" , method . toUpperCase ( ) , path ) ; return this . client . request ( method , this . normalizeURL ( path ) , clientOptions ) ; } param { string } path returns { string } private private normalizeURL ( path ) { const { href : normalizedURL } = new URL ( path , this . baseURL ) ; debug ( "formatted url: %s" , normalizedURL ) ; return normalizedURL ; } } export interface RequestAdapterOptions { serverURL : string ; token : string ; } export interface RequestOptions { params ? : { [ key ] : string | string [ ] | number [ ] ; } ; data ? : { [ key ] : any ; } | string ; headers ? : { [ key ] : string | number ; } ; timeout ? : number ; } export interface Response < T = any > { status : number ; data : T ; } export type HTTPMethod = "get" | "GET" | "delete" | "DELETE" | "head" | "HEAD" | "options" | "OPTIONS" | "post" | "POST" | "put" | "PUT" | "patch" | "PATCH" ;