Skip to content

Commit

Permalink
merge commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Barros authored and Gabriel Barros committed Oct 19, 2023
2 parents 448d0cd + 3b8fdef commit afc02ac
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed
- Master Data Builder usage
- New function that extends MD Client to use a specific schema

## [1.7.0] - 2023-10-09

## [1.6.1] - 2023-10-09
Expand Down
5 changes: 1 addition & 4 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"node": "6.x",
"docs": "0.x",
"react": "3.x",
"masterdata": "1.x",
"graphql": "1.x",
"store": "0.x",
"messages": "1.x",
Expand Down Expand Up @@ -97,9 +96,7 @@
},
"billingOptions": {
"type": "free",
"availableCountries": [
"*"
],
"availableCountries": ["*"],
"support": {
"url": "https://support.vtex.com/hc/requests"
}
Expand Down
11 changes: 9 additions & 2 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import AuthenticationClient from './authenticationClient'
import CheckoutExtended from './checkout'
import IdentityClient from './IdentityClient'
import VtexId from './vtexId'
import { withCustomSchema } from '../utils/withCustomSchema'

export class Clients extends IOClients {
public get affiliates() {
return this.getOrSet('affiliates', masterDataFor<Affiliates>('affiliates'))
return this.getOrSet(
'affiliates',
withCustomSchema('1.7.0', masterDataFor<Affiliates>('affiliates'))
)
}

public get userAffiliation() {
return this.getOrSet(
'userAffiliation',
masterDataFor<UserAffiliation>('userAffiliation')
withCustomSchema(
'1.7.0',
masterDataFor<UserAffiliation>('userAffiliation')
)
)
}

Expand Down
88 changes: 88 additions & 0 deletions node/utils/withCustomSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { InstanceOptions, IOContext } from '@vtex/api'
import type { PaginationArgs, WithMetadata } from '@vtex/clients'
import { MasterDataEntity } from '@vtex/clients'

type ScrollInput<K> = {
fields: Array<ThisType<K> | '_all'>
sort?: string
size?: number
mdToken?: string
}

export const withCustomSchema = <TEntity extends Record<string, unknown>>(
schema: string,
_MasterdataClient: new (
context: IOContext,
options?: InstanceOptions
) => MasterDataEntity<TEntity>
): new (
context: IOContext,
options?: InstanceOptions
) => MasterDataEntity<TEntity> => {
return class extends MasterDataEntity<TEntity> {
public dataEntity: string
public schema: string
public client: MasterDataEntity<TEntity>

constructor(ctx: IOContext, options?: InstanceOptions) {
super(ctx, options)
this.client = new _MasterdataClient(ctx, options)
this.dataEntity = this.client.dataEntity
this.schema = schema
this.client.schema = schema
}

public save(entity: TEntity) {
return this.client.save(entity)
}

public update(id: string, fields: Partial<TEntity>) {
return this.client.update(id, fields)
}

public saveOrUpdate(fields: TEntity & { id: string }) {
return this.client.saveOrUpdate(fields)
}

public delete(id: string) {
return this.client.delete(id)
}

// These es-lint disables are needed because I have to extend the class and its methods surpass the max params.
// eslint-disable-next-line max-params
public search<K extends keyof WithMetadata<TEntity>>(
pagination: PaginationArgs,
fields: Array<ThisType<K> | '_all'>,
sort?: string,
where?: string
): Promise<Array<Pick<WithMetadata<TEntity>, K>>> {
return this.client.search(pagination, fields, sort, where)
}

// eslint-disable-next-line max-params
public searchRaw<K extends keyof WithMetadata<TEntity>>(
pagination: PaginationArgs,
fields: Array<ThisType<K> | '_all'>,
sort?: string,
where?: string
): Promise<{
data: Array<Pick<WithMetadata<TEntity>, K>>
pagination: { total: number; page: number; pageSize: number }
}> {
return this.client.searchRaw(pagination, fields, sort, where)
}

public get<K extends keyof WithMetadata<TEntity>>(
id: string,
fields: Array<ThisType<K> | '_all'>
) {
return this.client.get(id, fields)
}

public async scroll<K extends keyof WithMetadata<TEntity>>(
input: ScrollInput<K>
) {
return this.client.scroll(input)
}
}
}

0 comments on commit afc02ac

Please sign in to comment.