From 7b8f35561a33d6973903fa42778673e86b4cfb83 Mon Sep 17 00:00:00 2001 From: ikolr-trika Date: Wed, 23 Jul 2025 21:10:57 +0530 Subject: [PATCH] Added custom end point --- manifest.json | 22 ++++++++++++++++++- node/clients/customDataEndpoint.ts | 35 ++++++++++++++++++++++++++++++ node/clients/index.ts | 4 ++++ node/config/index.ts | 5 +++++ node/handler/getEntitiesHandler.ts | 19 ++++++++++++++++ node/index.ts | 15 ++++++++----- node/package.json | 2 +- node/service.json | 5 +++-- node/yarn.lock | 10 ++++----- 9 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 node/clients/customDataEndpoint.ts create mode 100644 node/config/index.ts create mode 100644 node/handler/getEntitiesHandler.ts diff --git a/manifest.json b/manifest.json index 295415f..a66d77e 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "service-example", - "vendor": "vtex", + "vendor": "trika", "version": "0.3.0", "title": "Service Example", "description": "Reference app for VTEX IO Services", @@ -23,6 +23,26 @@ "path": "*" } }, + { + "name": "outbound-access", + "attrs": { + "host": "trika.vtexcommercestable.com.br", + "path": "*" + } + }, + { + "name": "outbound-access", + "attrs": { + "host": "api.io.vtex.com", + "path": "*" + } + }, + { + "name": "ADMIN_DS" + }, + { + "name": "POWER_USER_DS" + }, { "name": "colossus-fire-event" }, diff --git a/node/clients/customDataEndpoint.ts b/node/clients/customDataEndpoint.ts new file mode 100644 index 0000000..d02aed0 --- /dev/null +++ b/node/clients/customDataEndpoint.ts @@ -0,0 +1,35 @@ +import type { InstanceOptions, IOContext } from '@vtex/api' // IOResponse +import { ExternalClient } from '@vtex/api' +import { VTEX_APP_KEY, VTEX_APP_TOKEN, DATA_ENTITY } from '../config' + + +export default class ContactClient extends ExternalClient { + constructor(context: IOContext, options?: InstanceOptions) { + super(`https://${context.account}.vtexcommercestable.com.br`, context, { + ...options, + headers: { + 'X-VTEX-API-AppKey': VTEX_APP_KEY, + 'X-VTEX-API-AppToken': VTEX_APP_TOKEN, + 'Content-Type': 'application/json', + Accept: 'application/json', + } + }) + } + + public async getEntities(start: number = 0, end: number = 20): Promise { + console.log("running getEntities with start:", DATA_ENTITY, start, "end:", end); + + try { + const entities = await this.http.get(`/api/dataentities/${DATA_ENTITY}/search`,{ + params: { + _fields: '_all' + } + }) + + return entities + } catch (error) { + console.error('🔴 VTEX Master Data Error:', error.response?.data || error.message) + throw error + } +} +} diff --git a/node/clients/index.ts b/node/clients/index.ts index 0efe99f..3370775 100644 --- a/node/clients/index.ts +++ b/node/clients/index.ts @@ -1,4 +1,5 @@ import { IOClients } from '@vtex/api' +import ContactClient from './customDataEndpoint' import Status from './status' @@ -7,4 +8,7 @@ export class Clients extends IOClients { public get status() { return this.getOrSet('status', Status) } + public get contactClient() { + return this.getOrSet('contactClient', ContactClient) + } } diff --git a/node/config/index.ts b/node/config/index.ts new file mode 100644 index 0000000..d16a695 --- /dev/null +++ b/node/config/index.ts @@ -0,0 +1,5 @@ +export const VTEX_APP_KEY = process.env.VTEX_APP_KEY || 'vtexappkey-trika-ZYFCMI' +export const VTEX_APP_TOKEN = process.env.VTEX_APP_TOKEN || +'XAKBTWROXTLKANUXEXSRVPILGDOWSOOFQYAOEFAQWQNFGGEIMVGSJAFQODMTIIRUSLHVRGEDCNLQDGYTUYQCMOFCZXMVJEXENFHMFHRKPIPPXUIAGZFLLIMQPINBUYOQ' +export const ACCOUNT = process.env.VTEX_ACCOUNT || 'trika' +export const DATA_ENTITY = process.env.VTEX_DATA_ENTITY || 'LY' diff --git a/node/handler/getEntitiesHandler.ts b/node/handler/getEntitiesHandler.ts new file mode 100644 index 0000000..f340d04 --- /dev/null +++ b/node/handler/getEntitiesHandler.ts @@ -0,0 +1,19 @@ +import type { ServiceContext } from '@vtex/api' +import type { Clients } from '../clients/index' + +export async function getEntitiesHandler(ctx: ServiceContext, next: () => Promise) { + const {clients: { contactClient }} = ctx + try { + const data = await contactClient.getEntities() + + ctx.status = 200 + ctx.body = data + } catch (err) { + console.log('Running getEntitiesHandler') + + ctx.status = 500 + ctx.body = { error: 'Failed to fetch contact data', details: (err as Error).message } + } + + await next() +} diff --git a/node/index.ts b/node/index.ts index c4148f2..93703d1 100644 --- a/node/index.ts +++ b/node/index.ts @@ -2,8 +2,7 @@ import type { ClientsConfig, ServiceContext, RecorderState } from '@vtex/api' import { LRUCache, method, Service } from '@vtex/api' import { Clients } from './clients' -import { status } from './middlewares/status' -import { validate } from './middlewares/validate' +import { getEntitiesHandler } from './handler/getEntitiesHandler' const TIMEOUT_MS = 800 @@ -41,6 +40,12 @@ declare global { // The shape of our State object found in `ctx.state`. This is used as state bag to communicate between middlewares. interface State extends RecorderState { code: number + body: Settings | any + } + + type Settings = { + apiKey: string, + [key: string]: any } } @@ -48,9 +53,9 @@ declare global { export default new Service({ clients, routes: { - // `status` is the route ID from service.json. It maps to an array of middlewares (or a single handler). - status: method({ - GET: [validate, status], + + dataEntities: method({ + GET: [getEntitiesHandler], }), }, }) diff --git a/node/package.json b/node/package.json index 89baee9..c28caa7 100644 --- a/node/package.json +++ b/node/package.json @@ -8,7 +8,7 @@ "@types/jest": "^24.0.18", "@types/node": "^20.0.0", "@types/ramda": "types/npm-ramda#dist", - "@vtex/api": "6.47.0", + "@vtex/api": "6.48.0", "@vtex/test-tools": "^1.0.0", "@vtex/tsconfig": "^0.5.6", "typescript": "5.5.3" diff --git a/node/service.json b/node/service.json index 0de6302..6b33c42 100644 --- a/node/service.json +++ b/node/service.json @@ -6,8 +6,9 @@ "maxReplicas": 4, "workers": 1, "routes": { - "status": { - "path": "/_v/status/:code", + + "dataEntities": { + "path": "/_v/dataEntities", "public": true } } diff --git a/node/yarn.lock b/node/yarn.lock index ab258ca..3d95eed 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -1556,10 +1556,10 @@ resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.3.tgz#781d360c282436494b32fe7d9f7f8e64b3118aa3" integrity sha512-fbF6oTd4sGGy0xjHPKAt+eS2CrxJ3+6gQ3FGcBoIJR2TLAyCkCyI8JqZNy+FeON0AhVgNJoUumVoZQjBFUqHkw== -"@vtex/api@6.47.0": - version "6.47.0" - resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.47.0.tgz#6910455d593d8bb76f1f4f2b7660023853fda35e" - integrity sha512-t9gt7Q89EMbSj3rLhho+49Fv+/lQgiy8EPVRgtmmXFp1J4v8hIAZF7GPjCPie111KVs4eG0gfZFpmhA5dafKNA== +"@vtex/api@6.48.0": + version "6.48.0" + resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.48.0.tgz#67f9f11d197d543d4f854b057d31a8d6999241e9" + integrity sha512-mAdT7gbV0/BwiuqUkNH1E7KZqTUczT5NbBBZcPJq5kmTr73PUjbR9wh//70ryJo2EAdHlqIgqgwsCVpozenlhg== dependencies: "@types/koa" "^2.11.0" "@types/koa-compose" "^3.2.3" @@ -5604,7 +5604,7 @@ static-extend@^0.1.1: define-property "^0.2.5" object-copy "^0.1.0" -stats-lite@vtex/node-stats-lite#dist: +"stats-lite@github:vtex/node-stats-lite#dist": version "2.2.0" resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797" dependencies: