Skip to content

Commit

Permalink
observeAxios in apitoolkit class
Browse files Browse the repository at this point in the history
  • Loading branch information
dawkaka committed Apr 14, 2024
1 parent 03a8be8 commit 6b72934
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/apitoolkit.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PubSub } from '@google-cloud/pubsub';
import { ReportError } from 'apitoolkit-js';
import { AxiosInstance } from 'axios';
import { NextFunction, Request, Response } from 'express';
export type ATError = {
when: string;
Expand Down Expand Up @@ -57,6 +59,8 @@ export declare class APIToolkit {
static NewClient(config: Config): APIToolkit;
close(): Promise<void>;
static getClientMetadata(rootURL: string, apiKey: string): ClientMetadata;
observeAxios(axiosInstance: AxiosInstance, urlWildcard?: string | undefined, redactHeaders?: string[] | undefined, redactRequestBody?: string[] | undefined, redactResponseBody?: string[] | undefined): import("axios").AxiosInstance;
ReportError: typeof ReportError;
expressMiddleware(req: Request, res: Response, next: NextFunction): void;
errorHandler(err: any, req: Request, res: Response, next: NextFunction): void;
}
Expand Down
14 changes: 9 additions & 5 deletions lib/apitoolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class APIToolkit {
_APIToolkit_pubsub.set(this, void 0);
_APIToolkit_project_id.set(this, void 0);
_APIToolkit_config.set(this, void 0);
this.ReportError = apitoolkit_js_1.ReportError;
__classPrivateFieldSet(this, _APIToolkit_topicName, topicName, "f");
__classPrivateFieldSet(this, _APIToolkit_pubsub, pubsub, "f");
__classPrivateFieldSet(this, _APIToolkit_project_id, project_id, "f");
Expand Down Expand Up @@ -86,7 +87,10 @@ class APIToolkit {
});
if (!resp.ok)
throw new Error(`Error getting apitoolkit client_metadata ${resp.status}`);
return (resp.json());
return resp.json();
}
observeAxios(axiosInstance, urlWildcard, redactHeaders, redactRequestBody, redactResponseBody) {
return (0, apitoolkit_js_1.observeAxios)(axiosInstance, urlWildcard, redactHeaders, redactRequestBody, redactResponseBody, true);
}
expressMiddleware(req, res, next) {
if (!__classPrivateFieldGet(this, _APIToolkit_project_id, "f")) {
Expand Down Expand Up @@ -142,8 +146,8 @@ class APIToolkit {
reqBody = String(req.body);
}
}
let url_path = req.route?.path || "";
if (req.baseUrl && req.baseUrl != "") {
let url_path = req.route?.path || '';
if (req.baseUrl && req.baseUrl != '') {
url_path = req.baseUrl + url_path;
}
const errors = apitoolkit_js_1.asyncLocalStorage.getStore()?.get('AT_errors') ?? [];
Expand All @@ -152,7 +156,7 @@ class APIToolkit {
start_time,
requestHeaders: req.headers,
responseHeaders: res.getHeaders(),
sdk_type: "JsExpress",
sdk_type: 'JsExpress',
reqQuery: req.query,
raw_url: req.originalUrl,
url_path: url_path,
Expand All @@ -170,7 +174,7 @@ class APIToolkit {
service_version: __classPrivateFieldGet(this, _APIToolkit_config, "f")?.serviceVersion,
tags: __classPrivateFieldGet(this, _APIToolkit_config, "f")?.tags ?? [],
msg_id,
parent_id: undefined
parent_id: undefined,
});
if (__classPrivateFieldGet(this, _APIToolkit_config, "f")?.debug) {
console.log('APIToolkit: publish prepared payload ');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apitoolkit-express",
"version": "2.2.1",
"version": "2.2.2",
"description": "Nodejs SDK for the API Toolkit Monitoring and Reliability platform written in Typescript",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -41,7 +41,7 @@
"@types/supertest": "^2.0.15",
"@types/sync-fetch": "^0.4.2",
"@types/uuid": "^9.0.6",
"apitoolkit-js": "^3.2.5",
"apitoolkit-js": "^3.2.7",
"axios": "^1.6.0",
"busboy": "^1.6.0",
"express": "^4.18.2",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/apitoolkit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { PubSub, Topic } from '@google-cloud/pubsub';
import { asyncLocalStorage, buildPayload, ReportError } from 'apitoolkit-js';
import { asyncLocalStorage, buildPayload, observeAxios,ReportError } from 'apitoolkit-js';
import { AxiosInstance } from 'axios';
import { NextFunction, Request, Response } from 'express';
import fetch from 'sync-fetch';
import { v4 as uuidv4 } from 'uuid';
import { observeAxios } from 'apitoolkit-js';
import { AxiosInstance } from 'axios';

export type ATError = {
when: string; // timestamp
Expand Down

0 comments on commit 6b72934

Please sign in to comment.