@devgurusio/commercetools-sdk-repositories
v1.0.1
Published
commercetools SDK repositories
Downloads
671
Readme
@devgurusio/commercetools-sdk-repositories
@devgurusio/commercetools-sdk-repositories This library contains repositories to work with commercetools-sdk-typescript
- requestBuilder commercetools by project key request builder
Install
npm i @devgurusio/commercetools-sdk-repositories --save
Dependencies
This package requires @commercetools/sdk-client-v2 and @commercetools/platform-sdk
npm i @commercetools/sdk-client-v2 @commercetools/platform-sdk --save
Usage
Create a byProjectKeyRequest builder
JavaScript
const {
AuthMiddlewareOptions,
Client,
ClientBuilder,
HttpMiddlewareOptions,
} = require("@commercetools/sdk-client-v2");
const fetch = require("isomorphic-fetch");
const {
ApiRoot,
createApiBuilderFromCtpClient,
} = require("@commercetools/platform-sdk");
const { ByProjectKeyRequestBuilder } = require("@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder");
const auth = {
host: "https://auth.commercetools.co",
projectKey: "projectKey",
credentials: {
clientId: "clientId",
clientSecret: "clientSecret"
}
};
const http = {
host: "https://api.commercetools.co",
enableRetry: true,
retryConfig: {
maxRetries: 3
},
};
const queue = { concurrency: 10 };
const projectKey = "projectKey";
const httpMiddlewareOptions: HttpMiddlewareOptions = {
...http,
fetch: http.fetch || fetch,
};
const clientBuilder: ClientBuilder = new ClientBuilder()
.withProjectKey(projectKey)
.withHttpMiddleware(httpMiddlewareOptions)
.withLoggerMiddleware();
if (auth) {
const authMiddlewareOptions: AuthMiddlewareOptions = {
...auth,
fetch: auth.fetch || fetch,
};
clientBuilder.withClientCredentialsFlow(authMiddlewareOptions);
}
if (middleware) {
clientBuilder.withMiddleware(middleware);
}
if (queue) {
clientBuilder.withQueueMiddleware(queue);
}
const ctpClient: Client = clientBuilder.build();
const apiRoot: ApiRoot = createApiBuilderFromCtpClient(ctpClient);
const requestBuilder: ByProjectKeyRequestBuilder = apiRoot.withProjectKey({
projectKey,
});
TypeScript
import {
AuthMiddlewareOptions,
Client,
ClientBuilder,
HttpMiddlewareOptions,
} from "@commercetools/sdk-client-v2";
import fetch from "isomorphic-fetch";
import {
ApiRoot,
createApiBuilderFromCtpClient,
} from "@commercetools/platform-sdk";
import { ByProjectKeyRequestBuilder } from "@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder";
const auth = {
host: "https://auth.commercetools.co",
projectKey: "projectKey",
credentials: {
clientId: "clientId",
clientSecret: "clientSecret"
}
};
const http = {
host: "https://api.commercetools.co",
enableRetry: true,
retryConfig: {
maxRetries: 3
},
};
const queue = { concurrency: 10 };
const projectKey = "projectKey";
const httpMiddlewareOptions: HttpMiddlewareOptions = {
...http,
fetch: http.fetch || fetch,
};
const clientBuilder: ClientBuilder = new ClientBuilder()
.withProjectKey(projectKey)
.withHttpMiddleware(httpMiddlewareOptions)
.withLoggerMiddleware();
if (auth) {
const authMiddlewareOptions: AuthMiddlewareOptions = {
...auth,
fetch: auth.fetch || fetch,
};
clientBuilder.withClientCredentialsFlow(authMiddlewareOptions);
}
if (middleware) {
clientBuilder.withMiddleware(middleware);
}
if (queue) {
clientBuilder.withQueueMiddleware(queue);
}
const ctpClient: Client = clientBuilder.build();
const apiRoot: ApiRoot = createApiBuilderFromCtpClient(ctpClient);
const byProjectKeyRequestBuilder: ByProjectKeyRequestBuilder = apiRoot.withProjectKey({
projectKey,
});
Import and instantiate the repository you want to use
JavaScript
const { CartsRepository } = require("commercetools-sdk-client-repositories");
const cartsRepository = new CartsRepository(byProjectKeyRequestBuilder);
TypeScript
import { CartsRepository } from "commercetools-sdk-client-repositories";
const cartsRepository = new CartsRepository(byProjectKeyRequestBuilder);
Work with it
JavaScript
const cart = await cartsRepository.create({ currency: 'USD' });
TypeScript
const cart = await cartsRepository.create({ currency: 'USD' });