fastify-commercetools

1.2.0 • Public • Published

fastify-commercetools

commercetools fastify plugin that decorates fastify with commercetools key. commercetoos decorator contains:

Install

npm i fastify-commercetools --save

Usage

Register the plugin

Add it to your project with register and pass it some basic options.

const fastify = require("fastify")();

fastify.register(require("fastify-commercetools"), {
  commercetools: {
    host: "https://api.commercetools.co",
    oauthHost: "https://auth.commercetools.co",
    projectKey: "projectKey",
    clientId: "clientId",
    clientSecret: "clientSecret",
    concurrency: 5,
    allowedHeaders: []
  }
});

Once you have register the plugin you can use the commercetools decorator to perform actions

Using it as commercetools sdk

fastify.post("/", schemas.signUp, async (request, reply) => {
  const { client, requestBuilder } = fastify.commercetools;
  const { email, password } = request.body;

  try {
    const response = await client.execute({
      uri: requestBuilder().customers.build(),
      method: "POST",
      body: JSON.stringify({ email, password })
    });

    reply.code(200).send(response.body.customer);
  } catch (error) {
    handleCTError(request, reply, error);
  }
});

Using it as service

fastify.post("/", schemas.signUp, async (request, reply) => {
  const { CustomerRepository } = fastify.commercetools.repositories;
  const { email, password } = request.body;

  try {
    const customer = await CustomerRepository.create({ email, password });

    reply.code(200).send(customer);
  } catch (error) {
    handleCTError(request, reply, error);
  }
});

Package Sidebar

Install

npm i fastify-commercetools

Weekly Downloads

23

Version

1.2.0

License

ISC

Unpacked Size

636 kB

Total Files

82

Last publish

Collaborators

  • rubenbrieva
  • xemayebenes
  • fmoro