This library enables developers to quickly integrate with the VTEX IO APIs and create full fledged node services using VTEX IO.
For a complete example on using @vtex/api, check out this app: https://github.com/vtex-apps/service-example
The most basic usage is to export a new Service() with your route handlers:
// Import global typesimport'./globals'import{Service}from'@vtex/api'import{clients}from'./clients'importexamplefrom'./handlers/example'// Export a service that defines route handlers and client options.exportdefaultnewService({
clients,routes: {
example,},})This allows you to define middlewares that receive a Context param which contains all IO Clients in the clients property:
exportconstexample=async(ctx: Context,next: ()=>Promise<void>)=>{const{state: {code},clients: {apps}}=ctxconsole.log('Received code:',code)constapps=awaitapps.listApps()ctx.status=200ctx.body=appsctx.set('Cache-Control','private')awaitnext()}ctx.clients.apps is an instance of Apps.
- Install the dependencies:
yarn - Watch for changes:
yarn watch
- Install the dependencies:
yarn - Link this package:
yarn link - Watch for changes:
yarn watch - Move to the app that depends on the changes made on this package:
cd ../<your-app>/node - Link this package to your app's node_modules:
yarn link @vtex/api
Now, when you get a workspace up and running for your app with vtex link, you'll have this package linked as well.
When done developing, don't forget to unlink it from
<your-app>/node:yarn unlink @vtex/api