Answersheet to 'Getting to know more about clients'
// node/handler/analytics.ts
export async function analytics(ctx: Context, next: () => Promise<any>) {
const {
clients: { analytics },
} = ctx
ctx.status = 200
ctx.body = await analytics.getLiveUsers()
ctx.set('cache-control', 'no-cache')
await next()
}
// node/clients/analyticsClient.ts
import { AppClient, InstanceOptions, IOContext } from '@vtex/api'
export default class Analytics extends AppClient {
constructor(context: IOContext, options?: InstanceOptions) {
super('[email protected]', context, options)
}
public async getLiveUsers(): Promise<LiveUsersProduct[]> {
return this.http.get('_v/live-products')
}
}
export interface LiveUsersProduct {
slug: string
liveUsers: number
}
// node/clients/index.ts
import { IOClients } from '@vtex/api'
import Analytics from './analyticsClient'
export class Clients extends IOClients {
public get analytics() {
return this.getOrSet('analytics', Analytics)
}
}
Help us make this content better!
VTEX IO courses are open source. If you see something wrong, you can open a pull request!
Make a contributionUpdated almost 4 years ago