Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ekkolon/gelato-admin-node
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkolon committed Feb 20, 2024
2 parents fa363f3 + 156a289 commit af7cb2a
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 5 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ const myNamedClient = getClient('my-named-client');

Here is an overview of Gelato API services available in this library.

| Name | Module | Service |
| ------------ | ----------------------- | ------------------ |
| **Orders** | `gelato-admin/orders` | `getOrdersAPI()` |
| **Products** | `gelato-admin/products` | `getProductsAPI()` |
| **Shipment** | `gelato-admin/shipment` | `getShipmentAPI()` |
| Name | Module | Service |
| ------------- | ------------------------ | ------------------- |
| **Orders** | `gelato-admin/orders` | `getOrdersAPI()` |
| **Products** | `gelato-admin/products` | `getProductsAPI()` |
| **Shipment** | `gelato-admin/shipment` | `getShipmentAPI()` |
| **Ecommerce** | `gelato-admin/ecommerce` | `getEcommerceAPI()` |

### Default client

Expand Down
4 changes: 4 additions & 0 deletions entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"gelato-admin/shipment": {
"typings": "./lib/services/shipment/index.d.ts",
"dist": "./lib/services/shipment/index.js"
},
"gelato-admin/ecommerce": {
"typings": "./lib/services/ecommerce/index.d.ts",
"dist": "./lib/services/ecommerce/index.js"
}
}
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
],
"shipment": [
"lib/services/shipment"
],
"ecommerce": [
"lib/services/ecommerce"
]
}
},
Expand All @@ -72,6 +75,11 @@
"types": "./lib/services/shipment/index.d.ts",
"require": "./lib/services/shipment/index.js",
"import": "./lib/esm/shipment/index.js"
},
"./ecommerce": {
"types": "./lib/services/ecommerce/index.d.ts",
"require": "./lib/services/ecommerce/index.js",
"import": "./lib/esm/ecommerce/index.js"
}
},
"dependencies": {
Expand Down
89 changes: 89 additions & 0 deletions src/services/ecommerce/ecommerce-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*!
* @license
* Copyright 2023 Nelson Dominguez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { combineURLs } from '../../utils/urls';
import { ApiService } from '../api-service';
import {
CreateProductRequest,
CreateProductResponse,
GetProductListRequest,
GetProductListResponse,
GetProductResponse,
} from './products';
import { GetTemplateResponse } from './templates';

const ECOMMERCE_ROOT_URL = 'https://ecommerce.gelatoapis.com/v1';

const getEcommerceStoreProductsUrl = (storeId: string) => {
const ecommerceStoresURL = combineURLs(ECOMMERCE_ROOT_URL, 'stores');
return combineURLs(ecommerceStoresURL, storeId);
};

const ECOMMERCE_TEMPLATES_URL = combineURLs(ECOMMERCE_ROOT_URL, 'templates');

/**
* @description
*
*
* @resourceVersion `v1`
* @resourceURI `https://ecommerce.gelatoapis.com/v1`
*
* @publicApi
*/
export class EcommerceAPI extends ApiService {
/**
* Retrieve a list of products
* @param params Params to customize query result
*/
getProducts(storeId: string, params: GetProductListRequest): Promise<GetProductListResponse> {
const productsUrl = getEcommerceStoreProductsUrl(storeId);
return this.httpClient.get<GetProductListResponse>(productsUrl, { params });
}

/**
* Retrieve product information
* @param storeId The target ecommerce store ID.
* @param productId The ID of the product to fetch.
*/
getProduct(storeId: string, productId: string): Promise<CreateProductResponse> {
const productsUrl = getEcommerceStoreProductsUrl(storeId);
const productUrl = combineURLs(productsUrl, productId);
return this.httpClient.get<GetProductResponse>(productUrl);
}

/**
* Create a product from template.
* @param storeId The target ecommerce store ID.
* @param data Product data.
*/
createProductFromTemplate(
storeId: string,
data: CreateProductRequest,
): Promise<CreateProductResponse> {
const url = getEcommerceStoreProductsUrl(storeId);
return this.httpClient.post<CreateProductResponse, CreateProductRequest>(url, data);
}

/**
* Retrieve template information
* @param templateId The ID of the template to fetch
*/
getTemplate(templateId: string): Promise<GetTemplateResponse> {
const templateUrl = combineURLs(ECOMMERCE_TEMPLATES_URL, templateId);
return this.httpClient.get<GetTemplateResponse>(templateUrl);
}
}
54 changes: 54 additions & 0 deletions src/services/ecommerce/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* @license
* Copyright 2023 Nelson Dominguez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { GelatoClient } from '../../client/gelato-client';
import { Client, getClient } from '../../client/index';
import { EcommerceAPI } from './ecommerce-api';

/**
* Get a reference to the {@link EcommerceAPI} service for the default client or a
* given client.
*
* `getEcommerceAPI()` can be called with no arguments to access the default client's
* {@link EcommerceAPI} service or as `getEcommerceAPI(client)` to access the
* {@link EcommerceAPI} service associated with a specific client.
*
* @example
* ```javascript
* // Get the EcommerceAPI service for the default client
* const defaultEcommerceAPI = getEcommerceAPI();
* ```
*
* @example
* ```javascript
* // Get the EcommerceAPI service for a given client
* const otherEcommerceAPI = getEcommerceAPI(otherClient);
* ```
*
*/
export function getEcommerceAPI(client?: Client): EcommerceAPI {
if (typeof client === 'undefined') {
client = getClient();
}

const gelatoClient: GelatoClient = client as GelatoClient;
return gelatoClient.getOrInitService('ecommerce', (client) => new EcommerceAPI(client));
}

export { EcommerceAPI } from './ecommerce-api';
export * from './products';
export * from './templates';
Loading

0 comments on commit af7cb2a

Please sign in to comment.