Skip to content
Open
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
22 changes: 21 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
35 changes: 35 additions & 0 deletions node/clients/customDataEndpoint.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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
}
}
}
4 changes: 4 additions & 0 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IOClients } from '@vtex/api'
import ContactClient from './customDataEndpoint'

import Status from './status'

Expand All @@ -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)
}
}
5 changes: 5 additions & 0 deletions node/config/index.ts
Original file line number Diff line number Diff line change
@@ -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 ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way is wrong. We need to add config into app the then use it.

'XAKBTWROXTLKANUXEXSRVPILGDOWSOOFQYAOEFAQWQNFGGEIMVGSJAFQODMTIIRUSLHVRGEDCNLQDGYTUYQCMOFCZXMVJEXENFHMFHRKPIPPXUIAGZFLLIMQPINBUYOQ'
export const ACCOUNT = process.env.VTEX_ACCOUNT || 'trika'
export const DATA_ENTITY = process.env.VTEX_DATA_ENTITY || 'LY'
19 changes: 19 additions & 0 deletions node/handler/getEntitiesHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ServiceContext } from '@vtex/api'
import type { Clients } from '../clients/index'

export async function getEntitiesHandler(ctx: ServiceContext<Clients>, next: () => Promise<any>) {
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()
}
15 changes: 10 additions & 5 deletions node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -41,16 +40,22 @@ 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
}
}

// Export a service that defines route handlers and client options.
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({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data entities is very generic name, Its should more appropriate, Like contact, etc

GET: [getEntitiesHandler],
}),
},
})
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"maxReplicas": 4,
"workers": 1,
"routes": {
"status": {
"path": "/_v/status/:code",

"dataEntities": {
"path": "/_v/dataEntities",
"public": true
}
}
Expand Down
10 changes: 5 additions & 5 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file need to be ignored from getting pushed


"@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"
Expand Down Expand Up @@ -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:
Expand Down