Skip to content

Commit

Permalink
[Github NangoHQ#662] fix proxy params (NangoHQ#664)
Browse files Browse the repository at this point in the history
* [gh-NangoHQ#662] honor params

* [gh-NangoHQ#662] pass along query params
  • Loading branch information
khaliqgant authored Jun 7, 2023
1 parent 99d3245 commit 519fa4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
12 changes: 10 additions & 2 deletions packages/node-client/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios, { AxiosRequestConfig } from 'axios';
import { validateProxyConfiguration } from './utils.js';

import type { ProxyConfiguration } from './types';
Expand Down Expand Up @@ -93,10 +93,18 @@ export class Nango {
headers['Retries'] = retries;
}

const options = {
const options: AxiosRequestConfig = {
headers: this.enrichHeaders(headers)
};

if (config.params) {
options.params = config.params;
}

if (config.paramsSerializer) {
options.paramsSerializer = config.paramsSerializer;
}

if (method?.toUpperCase() === 'POST') {
return axios.post(url, config.data, options);
} else if (method?.toUpperCase() === 'PATCH') {
Expand Down
21 changes: 3 additions & 18 deletions packages/node-client/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ParamsSerializerOptions } from 'axios';

export interface ProxyConfiguration {
// allows for dynamic checking of required params
[key: string]: any;
Expand All @@ -9,24 +11,7 @@ export interface ProxyConfiguration {
method?: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE' | 'get' | 'post' | 'patch' | 'put' | 'delete';
headers?: Record<string, string>;
params?: string | Record<string, string>;
paramsSerializer?: {
encode?: (param: string) => string;
serialize?: (params: Record<string, any>, options?: ParamsSerializerOptions) => void;
indexes?: boolean;
};
paramsSerializer?: ParamsSerializerOptions;
data?: unknown;
retries?: number;
}

interface ParamsSerializerOptions {
encode?: ParamEncoder;
serialize?: CustomParamsSerializer;
}

interface ParamEncoder {
(value: any, defaultEncoder: (value: any) => any): any;
}

interface CustomParamsSerializer {
(params: Record<string, any>, options?: ParamsSerializerOptions): string;
}
10 changes: 7 additions & 3 deletions packages/server/lib/controllers/proxy.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Request, Response } from 'express';
import type { Request, Response, NextFunction } from 'express';
import type { OutgoingHttpHeaders } from 'http';
import type { NextFunction } from 'express';
import url, { UrlWithParsedQuery } from 'url';
import querystring from 'querystring';
import axios, { AxiosError, AxiosResponse } from 'axios';
import { backOff } from 'exponential-backoff';

Expand Down Expand Up @@ -100,7 +101,10 @@ class ProxyController {

const { method } = req;

const endpoint = req.params[0] as string;
const path = req.params[0] as string;
const { query }: UrlWithParsedQuery = url.parse(req.url, true) as unknown as UrlWithParsedQuery;
const queryString = querystring.stringify(query);
const endpoint = `${path}${queryString ? `?${queryString}` : ''}`;

if (!endpoint) {
errorManager.errRes(res, 'missing_endpoint');
Expand Down

0 comments on commit 519fa4b

Please sign in to comment.