Home / Class/ BaseAPI Class — typescript-sdk Architecture

BaseAPI Class — typescript-sdk Architecture

Architecture documentation for the BaseAPI class in runtime.ts from the typescript-sdk codebase.

Entity Profile

Dependency Diagram

graph TD
  fe132bbc_813e_ee3e_857d_412971a01950["BaseAPI"]
  41b6b5c2_aa5b_90b9_c373_84e0a8cd9918["runtime.ts"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|defined in| 41b6b5c2_aa5b_90b9_c373_84e0a8cd9918
  b2740552_8547_49da_0c85_5d41c2cb1ed1["constructor()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| b2740552_8547_49da_0c85_5d41c2cb1ed1
  aff18005_528b_5dee_bb93_2669c8f86dfd["withMiddleware()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| aff18005_528b_5dee_bb93_2669c8f86dfd
  3b211a63_7d27_411c_6fdc_7b98454c816b["withPreMiddleware()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| 3b211a63_7d27_411c_6fdc_7b98454c816b
  9ad38ef3_7f92_eca9_7c70_11684b40a76d["withPostMiddleware()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| 9ad38ef3_7f92_eca9_7c70_11684b40a76d
  8fc671c4_d25b_e4ca_3a86_40b3a31c1b5f["isJsonMime()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| 8fc671c4_d25b_e4ca_3a86_40b3a31c1b5f
  a9dbfef7_3f36_3873_c1c7_ad33a0dd92ef["request()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| a9dbfef7_3f36_3873_c1c7_ad33a0dd92ef
  a92f4938_2138_fb8a_a6dd_fabfe07a3d4f["createFetchParams()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| a92f4938_2138_fb8a_a6dd_fabfe07a3d4f
  d0152dfa_3b4e_4ea8_dcb6_f4455b23225d["clone()"]
  fe132bbc_813e_ee3e_857d_412971a01950 -->|method| d0152dfa_3b4e_4ea8_dcb6_f4455b23225d

Relationship Graph

Source Code

src/runtime.ts lines 92–250

export class BaseAPI {

    private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
    private middleware: Middleware[];

    constructor(protected configuration = DefaultConfig) {
        this.middleware = configuration.middleware;
    }

    withMiddleware<T extends BaseAPI>(this: T, ...middlewares: Middleware[]) {
        const next = this.clone<T>();
        next.middleware = next.middleware.concat(...middlewares);
        return next;
    }

    withPreMiddleware<T extends BaseAPI>(this: T, ...preMiddlewares: Array<Middleware['pre']>) {
        const middlewares = preMiddlewares.map((pre) => ({ pre }));
        return this.withMiddleware<T>(...middlewares);
    }

    withPostMiddleware<T extends BaseAPI>(this: T, ...postMiddlewares: Array<Middleware['post']>) {
        const middlewares = postMiddlewares.map((post) => ({ post }));
        return this.withMiddleware<T>(...middlewares);
    }

    /**
     * Check if the given MIME is a JSON MIME.
     * JSON MIME examples:
     *   application/json
     *   application/json; charset=UTF8
     *   APPLICATION/JSON
     *   application/vnd.company+json
     * @param mime - MIME (Multipurpose Internet Mail Extensions)
     * @return True if the given MIME is JSON, false otherwise.
     */
    protected isJsonMime(mime: string | null | undefined): boolean {
        if (!mime) {
            return false;
        }
        return BaseAPI.jsonRegex.test(mime);
    }

    protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
        const { url, init } = await this.createFetchParams(context, initOverrides);
        const response = await this.fetchApi(url, init);
        if (response && (response.status >= 200 && response.status < 300)) {
            return response;
        }
        throw new ResponseError(response, 'Response returned an error code');
    }

    private async createFetchParams(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction) {
        let url = this.configuration.basePath + context.path;
        if (context.query !== undefined && Object.keys(context.query).length !== 0) {
            // only add the querystring to the URL if there are query parameters.
            // this is done to avoid urls ending with a "?" character which buggy webservers
            // do not handle correctly sometimes.
            url += '?' + this.configuration.queryParamsStringify(context.query);
        }

        const headers = Object.assign({}, this.configuration.headers, context.headers);
        Object.keys(headers).forEach(key => headers[key] === undefined ? delete headers[key] : {});

        const initOverrideFn =
            typeof initOverrides === "function"
                ? initOverrides
                : async () => initOverrides;

        const initParams = {
            method: context.method,
            headers,
            body: context.body,
            credentials: this.configuration.credentials,
        };

        const overriddenInit: RequestInit = {
            ...initParams,
            ...(await initOverrideFn({
                init: initParams,
                context,
            }))

Domain

Defined In

Frequently Asked Questions

What is the BaseAPI class?
BaseAPI is a class in the typescript-sdk codebase, defined in src/runtime.ts.
Where is BaseAPI defined?
BaseAPI is defined in src/runtime.ts at line 92.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free