Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add new headers to request options #352

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Releases

## VERSION 2.1.0

- Add `X-Expand-Response-Nodes` header to request options
- Add `X-Card-Validation` header to request options
andreagostinho-meli marked this conversation as resolved.
Show resolved Hide resolved
- Add `X-Meli-Session-Id` header to request options

## VERSION 2.0.15

- Include `reference_id` in Data in Payment Method in Payment
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mercadopago",
"version": "2.0.15",
"version": "2.1.0",
"description": "Mercadopago SDK for Node.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
29 changes: 16 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
export declare type Config = {
accessToken: string;
options?: Options;
accessToken: string;
options?: Options;
};

export declare type Options = {
timeout?: number;
idempotencyKey?: string;
plataformId?: string;
integratorId?: string;
corporationId?: string;
timeout?: number;
idempotencyKey?: string;
plataformId?: string;
integratorId?: string;
corporationId?: string;
meliSessionId?: string;
expandResponseNodes?: string;
cardValidation?: string;
};

export declare interface SearchOptions {
limit?: number;
offset?: number;
[key: string]: string | number;
limit?: number;
offset?: number;
[key: string]: string | number;
}

export declare interface ApiResponse {
api_response: ResponseFields;
api_response: ResponseFields;
}

export declare type ResponseFields = {
status: number;
headers: [string, string[]];
status: number;
headers: [string, string[]];
};
9 changes: 6 additions & 3 deletions src/utils/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class AppConfig {
static readonly BASE_URL = 'https://api.mercadopago.com';
static readonly PRODUCT_ID = 'bc32b6ntrpp001u8nhkg';

static SDK_VERSION = '2.0.15';
static SDK_VERSION = '2.1.0';

static readonly Headers = {
AUTHORIZATION: 'Authorization',
Expand All @@ -16,7 +16,10 @@ export class AppConfig {
TRACKING_ID: 'X-Tracking-Id',
CORPORATION_ID: 'X-Corporation-Id',
INTEGRATOR_ID: 'X-Integrator-Id',
PLATFORM_ID: 'X-Platform-Id'
PLATFORM_ID: 'X-Platform-Id',
MELI_SESSION_ID: 'X-Meli-Session-Id',
EXPAND_RESPONSE_NODES: 'X-Expand-Response-Nodes',
CARD_VALIDATION: 'X-Card-Validation',
};

static getNodeVersion(): string {
Expand All @@ -32,7 +35,7 @@ export class AppConfig {
}

static getTrackingId(): string {
return 'platform:' + this.getNodeVersion().substring(0, this.getNodeVersion().indexOf('.')) + '|' + this.getNodeVersion() + ',type:SDK'+ this.SDK_VERSION + ',so;';
return 'platform:' + this.getNodeVersion().substring(0, this.getNodeVersion().indexOf('.')) + '|' + this.getNodeVersion() + ',type:SDK' + this.SDK_VERSION + ',so;';
}

static getUserAgent(): string {
Expand Down
10 changes: 8 additions & 2 deletions src/utils/restClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { v4 as uuidv4 } from 'uuid';
import type { Options } from '@src/types';

interface RestClientConfig extends Options {
queryParams?: Record<string, string | number>;
retries?: number;
queryParams?: Record<string, string | number>;
retries?: number;
}

class RestClient {
Expand Down Expand Up @@ -66,6 +66,9 @@ class RestClient {
corporationId,
integratorId,
plataformId,
meliSessionId,
expandResponseNodes,
cardValidation,
...customConfig
} = config || {};

Expand All @@ -79,6 +82,9 @@ class RestClient {
...(corporationId ? { [AppConfig.Headers.CORPORATION_ID]: corporationId } : {}),
...(integratorId ? { [AppConfig.Headers.INTEGRATOR_ID]: integratorId } : {}),
...(plataformId ? { [AppConfig.Headers.PLATFORM_ID]: plataformId } : {}),
...(meliSessionId ? { [AppConfig.Headers.MELI_SESSION_ID]: meliSessionId } : {}),
...(expandResponseNodes ? { [AppConfig.Headers.EXPAND_RESPONSE_NODES]: expandResponseNodes } : {}),
...(cardValidation ? { [AppConfig.Headers.CARD_VALIDATION]: cardValidation } : {}),
};

if (method && method !== 'GET') {
Expand Down
20 changes: 14 additions & 6 deletions src/utils/restClient/restClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ describe('RestClient', () => {
expect(fetch).toHaveBeenCalledTimes(retries);
expect(response).toEqual({
success: true,
api_response: {
headers: {
api_response: {
headers: {
'Content-Type': [
'text/plain;charset=UTF-8',
],
Expand All @@ -98,12 +98,17 @@ describe('RestClient', () => {
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue(
new Response(JSON.stringify({ success: true }), { url: 'url', status: 200, statusText: 'OK' })
);

const customHeaders = {
Authorization: 'Bearer Token123',
};
const endpoint = '/test-custom-headers';
await RestClient.fetch(endpoint, { headers: customHeaders });

await RestClient.fetch(endpoint, {
headers: customHeaders,
expandResponseNodes: 'gateway.reference',
cardValidation: 'card_validation',
meliSessionId: 'device_id',
});

expect(fetch).toHaveBeenCalledWith(expect.any(String), {
method: 'GET',
Expand All @@ -114,6 +119,9 @@ describe('RestClient', () => {
'User-Agent': expect.any(String),
'X-Product-Id': expect.any(String),
'X-Tracking-Id': expect.any(String),
'X-Meli-Session-Id': 'device_id',
'X-Expand-Response-Nodes': 'gateway.reference',
'X-Card-Validation': 'card_validation',
},
});
});
Expand Down Expand Up @@ -181,8 +189,8 @@ describe('RestClient', () => {
expect(fetch).toHaveBeenCalledTimes(4);
expect(response).toEqual({
success: true,
api_response: {
headers: {
api_response: {
headers: {
'Content-Type': [
'text/plain;charset=UTF-8',
],
Expand Down
Loading