From 17cb3e4d0b5e9949782ca42506f7917847da23dd Mon Sep 17 00:00:00 2001 From: andreagostinho-meli Date: Wed, 4 Dec 2024 12:26:38 -0300 Subject: [PATCH 1/3] feat: add X-Expand-Response-Nodes and X-Card-Validation headers --- src/types.ts | 28 +++++++++++++------------ src/utils/config/index.ts | 6 ++++-- src/utils/restClient/index.ts | 8 +++++-- src/utils/restClient/restClient.spec.ts | 18 ++++++++++------ 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/types.ts b/src/types.ts index f7b664c8..dcd56647 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,27 +1,29 @@ 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; + 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[]]; }; diff --git a/src/utils/config/index.ts b/src/utils/config/index.ts index 499f05c7..9146b7ea 100644 --- a/src/utils/config/index.ts +++ b/src/utils/config/index.ts @@ -16,7 +16,9 @@ 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', + EXPAND_RESPONSE_NODES: 'X-Expand-Response-Nodes', + CARD_VALIDATION: 'X-Card-Validation', }; static getNodeVersion(): string { @@ -32,7 +34,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 { diff --git a/src/utils/restClient/index.ts b/src/utils/restClient/index.ts index 596c2012..6bbd3766 100644 --- a/src/utils/restClient/index.ts +++ b/src/utils/restClient/index.ts @@ -5,8 +5,8 @@ import { v4 as uuidv4 } from 'uuid'; import type { Options } from '@src/types'; interface RestClientConfig extends Options { - queryParams?: Record; - retries?: number; + queryParams?: Record; + retries?: number; } class RestClient { @@ -66,6 +66,8 @@ class RestClient { corporationId, integratorId, plataformId, + expandResponseNodes, + cardValidation, ...customConfig } = config || {}; @@ -79,6 +81,8 @@ class RestClient { ...(corporationId ? { [AppConfig.Headers.CORPORATION_ID]: corporationId } : {}), ...(integratorId ? { [AppConfig.Headers.INTEGRATOR_ID]: integratorId } : {}), ...(plataformId ? { [AppConfig.Headers.PLATFORM_ID]: plataformId } : {}), + ...(expandResponseNodes ? { [AppConfig.Headers.EXPAND_RESPONSE_NODES]: expandResponseNodes } : {}), + ...(cardValidation ? { [AppConfig.Headers.CARD_VALIDATION]: cardValidation } : {}), }; if (method && method !== 'GET') { diff --git a/src/utils/restClient/restClient.spec.ts b/src/utils/restClient/restClient.spec.ts index 08c639ea..f8deb910 100644 --- a/src/utils/restClient/restClient.spec.ts +++ b/src/utils/restClient/restClient.spec.ts @@ -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', ], @@ -98,12 +98,16 @@ describe('RestClient', () => { (fetch as jest.MockedFunction).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', + }); expect(fetch).toHaveBeenCalledWith(expect.any(String), { method: 'GET', @@ -114,6 +118,8 @@ describe('RestClient', () => { 'User-Agent': expect.any(String), 'X-Product-Id': expect.any(String), 'X-Tracking-Id': expect.any(String), + 'X-Expand-Response-Nodes': 'gateway.reference', + 'X-Card-Validation': 'card_validation', }, }); }); @@ -181,8 +187,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', ], From 7bc918926b9ebc7f91cfb7faad23ecda12f7b21c Mon Sep 17 00:00:00 2001 From: andreagostinho-meli Date: Wed, 4 Dec 2024 12:33:04 -0300 Subject: [PATCH 2/3] bump sdk version to 2.1.0 --- CHANGELOG.MD | 5 +++++ package.json | 2 +- src/utils/config/index.ts | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 00499d20..941f8270 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,5 +1,10 @@ # Releases +## VERSION 2.1.0 + +- Add `X-Expand-Response-Nodes` header to request options +- Add `X-Card-Validation` header to request options + ## VERSION 2.0.15 - Include `reference_id` in Data in Payment Method in Payment diff --git a/package.json b/package.json index beb3ec9a..08f9d491 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/config/index.ts b/src/utils/config/index.ts index 9146b7ea..2e4d4465 100644 --- a/src/utils/config/index.ts +++ b/src/utils/config/index.ts @@ -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', From 218fce6012db54edc571cce76797b5cec3d57b32 Mon Sep 17 00:00:00 2001 From: andreagostinho-meli Date: Wed, 4 Dec 2024 14:42:15 -0300 Subject: [PATCH 3/3] feat: add header X-Meli-Session-Id --- CHANGELOG.MD | 1 + src/types.ts | 1 + src/utils/config/index.ts | 1 + src/utils/restClient/index.ts | 2 ++ src/utils/restClient/restClient.spec.ts | 2 ++ 5 files changed, 7 insertions(+) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 941f8270..24a93b8a 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -4,6 +4,7 @@ - Add `X-Expand-Response-Nodes` header to request options - Add `X-Card-Validation` header to request options +- Add `X-Meli-Session-Id` header to request options ## VERSION 2.0.15 diff --git a/src/types.ts b/src/types.ts index dcd56647..240273aa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,7 @@ export declare type Options = { plataformId?: string; integratorId?: string; corporationId?: string; + meliSessionId?: string; expandResponseNodes?: string; cardValidation?: string; }; diff --git a/src/utils/config/index.ts b/src/utils/config/index.ts index 2e4d4465..8456da54 100644 --- a/src/utils/config/index.ts +++ b/src/utils/config/index.ts @@ -17,6 +17,7 @@ export class AppConfig { CORPORATION_ID: 'X-Corporation-Id', INTEGRATOR_ID: 'X-Integrator-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', }; diff --git a/src/utils/restClient/index.ts b/src/utils/restClient/index.ts index 6bbd3766..bcb37a7c 100644 --- a/src/utils/restClient/index.ts +++ b/src/utils/restClient/index.ts @@ -66,6 +66,7 @@ class RestClient { corporationId, integratorId, plataformId, + meliSessionId, expandResponseNodes, cardValidation, ...customConfig @@ -81,6 +82,7 @@ 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 } : {}), }; diff --git a/src/utils/restClient/restClient.spec.ts b/src/utils/restClient/restClient.spec.ts index f8deb910..97c01268 100644 --- a/src/utils/restClient/restClient.spec.ts +++ b/src/utils/restClient/restClient.spec.ts @@ -107,6 +107,7 @@ describe('RestClient', () => { headers: customHeaders, expandResponseNodes: 'gateway.reference', cardValidation: 'card_validation', + meliSessionId: 'device_id', }); expect(fetch).toHaveBeenCalledWith(expect.any(String), { @@ -118,6 +119,7 @@ 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', },