Skip to content

Export necessary interface and class for customize http client implemendation#86

Open
lkebin wants to merge 1 commit into
Dynatrace:mainfrom
lkebin:export-interface-for-customize-impl
Open

Export necessary interface and class for customize http client implemendation#86
lkebin wants to merge 1 commit into
Dynatrace:mainfrom
lkebin:export-interface-for-customize-impl

Conversation

@lkebin

@lkebin lkebin commented May 23, 2025

Copy link
Copy Markdown

Some applications doesn't support standard fetch api. such as Wechat Miniprogram.

…entation

Some application doesn't support standard `fetch` api. such as Wechat
Miniprogram.
@TheHighriser

Copy link
Copy Markdown
Contributor

Did you see: https://github.com/Dynatrace/openkit-js/blob/main/src/OpenKitBuilder.ts#L170

Which allows you to fully customize the whole communication channel?

@lkebin

lkebin commented May 28, 2025

Copy link
Copy Markdown
Author

Yes, but I need export more info for custom HttpClient implementation. It's easier than implements whole customize communication channel, right ?

There's my implements.

import { HttpClient, HttpResponse, Logger, LoggerFactory } from "@dynatrace/openkit-js";
import 'miniprogram-api-typings';

export class WxHttpClient implements HttpClient {
    private readonly logger: Logger;

    constructor(loggerFactory: LoggerFactory) {
        this.logger = loggerFactory.createLogger('WxHttpClient');
    }

    public async get(url: string): Promise<HttpResponse> {
        this.logger.debug('GET', url);

        const response = await new Promise<WechatMiniprogram.RequestSuccessCallbackResult<string>>((resolve, reject) => {
          wx.request({
            url: url,
            method: 'GET',
            // Wechat disallow customize user-agent header of request
            //
            // header: {
            //   'User-Agent': 'OpenKit/' + openKitVersion,
            // },
            dataType: '其他',
            success: (res: WechatMiniprogram.RequestSuccessCallbackResult<string>) => {
              resolve(res)
            },
            fail: (res) => {
              reject(res)
            },
          })
        })

        return this.parseFetchResponse(response);
    }

    public async post(url: string, payload: string): Promise<HttpResponse> {
        this.logger.debug('POST', url, payload);

        const response = await new Promise<WechatMiniprogram.RequestSuccessCallbackResult<string>>((resolve, reject) => {
          wx.request({
            url: url,
            method: 'POST',
            data: payload,
            // Wechat disallow customize user-agent header of request
            //
            // header: {
            //   'User-Agent': 'OpenKit/' + openKitVersion,
            // },
            dataType: '其他',
            success: (res: WechatMiniprogram.RequestSuccessCallbackResult<string>) => {
              resolve(res)
            },
            fail: (res) => {
              reject(res)
            },
          })
        })

        return this.parseFetchResponse(response);
    }

    private async parseFetchResponse(
        response: WechatMiniprogram.RequestSuccessCallbackResult<string>,
    ): Promise<HttpResponse> {
        const responseString = response.data;

        this.logger.debug('RESPONSE', {
            status: response.statusCode,
            payload: responseString,
        });

        const headers: Record<string, string> = {};

        Object.entries(response.header).forEach(([key, value]) => {
            if (value != null) {
                headers[key] = value.toString();
            }
        });

        return {
            status: response.statusCode,
            payload: responseString,
            headers,
        };
    }
}

@TheHighriser

TheHighriser commented Jun 2, 2025

Copy link
Copy Markdown
Contributor

I can follow your idea. Although this is not an issue, more like a feature request. So there needs to be an official feature request via the Dynatrace platform, in the meantime feel free to modify the library as it is open source anyways.

@lkebin

lkebin commented Jun 3, 2025

Copy link
Copy Markdown
Author

OK, will use forked version for customize platform until official supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants