palantir / conjure-typescript-runtime

An HTTP bridge library for use in front end applications and generated conjure code
Apache License 2.0
5 stars 13 forks source link

Produce an optimized type replacement for IHttpApiBridge #162

Open ericanderson opened 1 year ago

ericanderson commented 1 year ago

There is no need for the current type to be an object, when it can be a function. In order to manage backwards compatibility, we can create this new type/implementation as required only for a future defined 'functions only' generator.

I envision this looking like

export type ModernHttpApiArgs = [
    /** Conjure service name. Doesn't affect the network request. */
    serviceName: string | undefined, 
    /** Conjure endpoint name. Doesn't affect the network request. */
    endpointName: string | undefined, 
    /** HTTP method. */
    method: string, 
    /** Path to make a request to, e.g. "/foo/{param1}/bar". */
    endpointPath: string, 
    /** Data to send in the body. */
    data?: any, 
    /** HTTP headers. */
    headers?: {
        [header: string]: string | number | boolean | undefined | null;
    }, 
    /** Key-value mappings to be appended to the request query string. */
    queryArguments?: {
        [paramName: string]: any;
    }, 
    /** Values to be interpolated into the endpointPath. */
    pathArguments?: any[], 
    /** MIME type of the outgoing request, if absent defaults to "application/json" */
    requestMediaType?: string, 
    /** MIME type of the expected server response, if absent defaults to "application/json" */
    responseMediaType?: string
]
export type ModernHttpApiBridge<T> = (args: ModernHttpApiArgs) => Promise<T>;

Note: This is a tuple instead of an array of arguments. This will allow invocation like:

const bridge: ModernHttpApiBridge = createBridge(...);
bridge([,,"POST", "/url", {data: 5},,,,"application/json"]);

This will allow us to omit arguments without resorting to the _undefined hack we currently use to compress better.