Skip to content

Commit

Permalink
Schemas client created
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Barros authored and Gabriel Barros committed Sep 22, 2023
1 parent 634b717 commit de509b0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
5 changes: 5 additions & 0 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Affiliates, UserAffiliation } from 'vtex.affiliates'

import AuthenticationClient from './authenticationClient'
import CheckoutExtended from './checkout'
import SchemasClient from './schemas'

export class Clients extends IOClients {
public get affiliates() {
Expand All @@ -24,4 +25,8 @@ export class Clients extends IOClients {
public get authentication() {
return this.getOrSet('authentication', AuthenticationClient)
}

public get schemas() {
return this.getOrSet('schemas', SchemasClient)
}
}
33 changes: 33 additions & 0 deletions node/clients/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { InstanceOptions, IOContext, RequestConfig } from '@vtex/api'
import { ExternalClient } from '@vtex/api'

const routes = {
schemas: (dataEntity: string) => `/dataentities/${dataEntity}/schemas`,
delete: (dataEntity: string, schemaName: string) =>
`dataentities/${dataEntity}/schemas/${schemaName}`,
}

export default class SchemasClient extends ExternalClient {
constructor(context: IOContext, options?: InstanceOptions) {
super(`http://${context.account}.myvtex.com/api`, context, {
...options,
headers: {
...options?.headers,
VtexIdclientAutCookie: context.authToken,
'Content-Type': 'application/json',
},
})
}

public getSchemas(dataEntity: string, config?: RequestConfig): Promise<any> {

Check warning on line 22 in node/clients/schemas.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
return this.http.get(routes.schemas(dataEntity), config)
}

public deleteSchemas(
dataEntity: string,
schemaName: string,
config?: RequestConfig
): Promise<any> {

Check warning on line 30 in node/clients/schemas.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
return this.http.delete(routes.delete(dataEntity, schemaName), config)
}
}
4 changes: 4 additions & 0 deletions node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { getAffiliate } from './resolvers/getAffiliate'
import { fieldResolvers } from './resolvers/fieldResolvers'
import { getAffiliateByEmail } from './resolvers/getAffiliateByEmail'
import { getWorkspaces } from './resolvers/getWorkspaces'
import { getSchemas } from './middlewares/getSchemas'

const TIMEOUT_MS = 1000

Expand Down Expand Up @@ -90,6 +91,9 @@ declare global {
export default new Service({
clients,
routes: {
schemas: method({
GET: [getSchemas],
}),
affiliate: method({
POST: [authenticateRequest, validateCreate, createAffiliate],
PATCH: [authenticateRequest, validateUpdate, updateAffiliate],
Expand Down
22 changes: 22 additions & 0 deletions node/middlewares/getSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export async function getSchemas(ctx: Context, next: () => Promise<unknown>) {
const {
clients: { schemas },
vtex: { logger },
} = ctx

try {
const schemaList = await schemas.getSchemas('vtex_affiliates_Affiliates')

console.info('testing', schemaList)
ctx.body = schemaList
ctx.status = 200
} catch (err) {
logger.error({
metric: 'get-affiliate-schema',
message: err.message,
})
throw new Error('Error getting affiliate schemas')
}

await next()
}
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@types/jest": "^27.0.2",
"@types/node": "^12.0.0",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "6.45.21",
"@vtex/api": "6.45.22",
"@vtex/test-tools": "^3.4.1",
"@vtex/tsconfig": "^0.5.6",
"typescript": "3.9.7"
Expand Down
4 changes: 4 additions & 0 deletions node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"maxReplicas": 4,
"workers": 1,
"routes": {
"schemas": {
"path": "/_v/affiliateSchemas",
"public": true
},
"affiliate": {
"path": "/_v/affiliate",
"public": true
Expand Down
8 changes: 4 additions & 4 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1658,10 +1658,10 @@
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.3.tgz#781d360c282436494b32fe7d9f7f8e64b3118aa3"
integrity sha512-fbF6oTd4sGGy0xjHPKAt+eS2CrxJ3+6gQ3FGcBoIJR2TLAyCkCyI8JqZNy+FeON0AhVgNJoUumVoZQjBFUqHkw==

"@vtex/[email protected].21":
version "6.45.21"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.21.tgz#2f5ea4a14dfbf426f6fbad140caf23c89060b313"
integrity sha512-Yo4og5bSM1n5WSQ3u8nsqEHicvxdyOZX3uA0KtQHbS4frSeA4hcGRuupsFHQ13eZZGMy4OR4fJO7ZQfkj8qLhQ==
"@vtex/[email protected].22":
version "6.45.22"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.22.tgz#fa9bbfde1a4d4fbbaf6cce9f6dbc9bb9ee929ba3"
integrity sha512-g5cGUDhF4FADgSMpQmce/bnIZumwGlPG2cabwbQKIQ+cCFMZqOEM/n+YQb1+S8bCyHkzW3u/ZABoyCKi5/nxxg==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down

0 comments on commit de509b0

Please sign in to comment.