diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9440978 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +help: ## Display this help menu + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +check: + npm run build + +gitclean: ## Rebase & Clean git + git fetch upstream + git rebase "upstream/main" + git push origin + git fetch --prune + +.DEFAULT_GOAL := help \ No newline at end of file diff --git a/e2e/src/fixtures/categories-sample.ts b/e2e/src/fixtures/categories-sample.ts index 0ff19a5..c169713 100644 --- a/e2e/src/fixtures/categories-sample.ts +++ b/e2e/src/fixtures/categories-sample.ts @@ -1,6 +1,6 @@ -import { CategoryWritable } from 'prestashop-ws-client'; +import { WSCategoryWritable } from 'prestashop-ws-client'; import { getLanguageValues } from 'prestashop-ws-client/src/xml/xml.interfaces'; -export const newCategory: CategoryWritable = { +export const newCategory: WSCategoryWritable = { id_parent: 3, active: 1, id_shop_default: '1', @@ -22,7 +22,7 @@ export const newCategory: CategoryWritable = { }, }; -export const categoryMultilanguage: CategoryWritable = { +export const categoryMultilanguage: WSCategoryWritable = { id: 4, id_parent: 3, active: 1, diff --git a/package.json b/package.json index 0bdb2b2..28999e7 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "./package.json": "./package.json" }, "scripts": { - "build": "pnpm run build:cjs && pnpm run build:esm", + "build": "npm run build:cjs && npm run build:esm", "build:esm": "rollup --bundleConfigAsCjs --config ./rollup.config.js", "build:cjs": "tsc -p ./tsconfig.cjs.json", "test": "jest", @@ -27,8 +27,9 @@ "lint": "eslint 'src/**/*.ts'", "format:fix": "prettier --write 'src/**/*.ts'", "lint:fix": "eslint 'src/**/*.ts' --fix", - "fix": "pnpm format:fix && pnpm lint:fix", - "example": "ts-node ./examples/test.ts" + "fix": "npm format:fix && npm lint:fix", + "example": "ts-node ./examples/test.ts", + "prepare": "npm run build" }, "repository": { "type": "git", diff --git a/src/types/entity-mapping.type.ts b/src/types/entity-mapping.type.ts index 6f776c4..8468f18 100644 --- a/src/types/entity-mapping.type.ts +++ b/src/types/entity-mapping.type.ts @@ -1,15 +1,15 @@ -import { Category, CategoryWritable } from './ws-entities/categories.type'; -import { Order } from './ws-entities/orders.type'; -import { Product, ProductWritable } from './ws-entities/products.type'; +import { WSCategory, WSCategoryWritable } from './ws-entities/categories.type'; +import { WSOrder } from './ws-entities/orders.type'; +import { WSProduct, WSProductWritable } from './ws-entities/products.type'; -export type Entity = { - categories: Category; - orders: Order; - products: Product; +export type WSEntity = { + categories: WSCategory; + orders: WSOrder; + products: WSProduct; }; -export type EntityWritable = { - categories: CategoryWritable; - orders: Order; - products: ProductWritable; +export type WSEntityWritable = { + categories: WSCategoryWritable; + orders: WSOrder; + products: WSProductWritable; }; diff --git a/src/types/ws-entities/categories.type.ts b/src/types/ws-entities/categories.type.ts index e3e295f..ed2d632 100644 --- a/src/types/ws-entities/categories.type.ts +++ b/src/types/ws-entities/categories.type.ts @@ -2,7 +2,7 @@ import { LanguageValue } from '../../xml/xml.interfaces'; -export type Category = { +export type WSCategory = { active: number; additional_description?: LanguageValue[] | string; associations?: { @@ -27,7 +27,7 @@ export type Category = { }; //Writable -export type CategoryWritable = Omit< - Category, +export type WSCategoryWritable = Omit< + WSCategory, 'level_depth' | 'nb_products_recursive' >; diff --git a/src/types/ws-entities/orders.type.ts b/src/types/ws-entities/orders.type.ts index ad53fc1..04e408e 100644 --- a/src/types/ws-entities/orders.type.ts +++ b/src/types/ws-entities/orders.type.ts @@ -1,4 +1,4 @@ -export type Order = { +export type WSOrder = { associations?: { order_rows?: OrderRow[] }; carrier_tax_rate: string; conversion_rate: string; diff --git a/src/types/ws-entities/products.type.ts b/src/types/ws-entities/products.type.ts index e22a579..3cea5cb 100644 --- a/src/types/ws-entities/products.type.ts +++ b/src/types/ws-entities/products.type.ts @@ -1,6 +1,6 @@ import { LanguageValue } from '../../xml/xml.interfaces'; -export type Product = { +export type WSProduct = { active: string; additional_delivery_times: string; additional_shipping_cost: string; @@ -93,4 +93,7 @@ export type Product = { width: string; }; -export type ProductWritable = Omit; +export type WSProductWritable = Omit< + WSProduct, + 'manufacturer_name' | 'quantity' +>; diff --git a/src/ws/ws-base-client.ts b/src/ws/ws-base-client.ts index 7e137bc..f5daca1 100644 --- a/src/ws/ws-base-client.ts +++ b/src/ws/ws-base-client.ts @@ -1,20 +1,17 @@ import { DOMParser } from '@xmldom/xmldom'; import { create } from 'xmlbuilder2'; import { Config } from '../types/config.type'; -import { Entity, EntityWritable } from '../types/entity-mapping.type'; +import { WSEntity, WSEntityWritable } from '../types/entity-mapping.type'; import { endpointNodes } from '../xml/endpoint-nodes'; import { getLanguageValues } from '../xml/xml.interfaces'; import { wsConfig } from './ws-config'; -/** - * - */ export interface RequestOptions { id?: string; query?: Record; } -export class BaseClient { +export class BaseClient { private languageIds: number | number[] = -1; private requiredNodes: string[] = []; private readOnlyNodes: string[] = []; @@ -23,7 +20,6 @@ export class BaseClient { constructor(private readonly endpoint: T) {} /** - * * @param {string} endpoint - Endpoint * @param {RequestOptions} options - Query parameters */ @@ -60,14 +56,14 @@ export class BaseClient { /** * - * @param {Partial} entityData - Data for the entity to be created + * @param {Partial} entityData - Data for the entity to be created */ - async create(entityData: Partial): Promise { + async create(entityData: Partial): Promise { if (this.getRequiredFields().length == 0) { //async ressources await this.initSpecificEntityFieldsIndicators(); } - const entity: Entity[T] = await this.getBlank(); + const entity: WSEntity[T] = await this.getJSONBlank(); this.fillFields(entity, entityData); this.removeReadOnlyFields(entity, entityData); @@ -94,14 +90,14 @@ export class BaseClient { }, ); const result = await response.json(); - return result[this.getEndPoint()][0] as Entity[T]; + return result[this.getEndPoint()][0] as WSEntity[T]; } /** * * @param { string } id - ID's entity */ - async get(id: string): Promise { + async get(id: string): Promise { const response: Response = await fetch( this.getUrl(this.getEndPoint(), { id: id, @@ -118,10 +114,10 @@ export class BaseClient { }, ); const json = await response.json(); - return json[this.getEndPoint()][0] as Entity[T]; + return json[this.getEndPoint()][0] as WSEntity[T]; } - async getAll(): Promise { + async getAll(): Promise { const response: Response = await fetch( this.getUrl(this.getEndPoint(), { query: { @@ -137,20 +133,20 @@ export class BaseClient { }, ); const json = await response.json(); - return json[this.getEndPoint()][0] as Entity[T]; + return json[this.getEndPoint()][0] as WSEntity[T]; } /** * * @param {Partial} entityData - Data for the entity to be updated */ - async update(entityData: Partial): Promise { + async update(entityData: Partial): Promise { if (this.getRequiredFields().length == 0) { //async ressources await this.initSpecificEntityFieldsIndicators(); } const entityId = entityData.id; - const entity: Entity[T] = await this.get(entityId!.toString()); + const entity: WSEntity[T] = await this.get(entityId!.toString()); this.fillFields(entity, entityData); this.removeReadOnlyFields(entity, entityData); @@ -176,7 +172,7 @@ export class BaseClient { body: xml, }, ); - return (await response.json()) as Entity[T]; + return (await response.json()) as WSEntity[T]; } async delete(id: string): Promise { @@ -242,9 +238,9 @@ export class BaseClient { } /** - * ?schema=blank: returns a blank Json tree of the chosen resource. + * ?schema=blank&output_format=JSON: returns a blank Json tree of the chosen resource. */ - async getBlank(): Promise { + async getJSONBlank(): Promise { const response: Response = await fetch( this.getUrl(this.getEndPoint(), { query: { @@ -262,7 +258,30 @@ export class BaseClient { ); const json = await response.json(); - return json[this.getEndPoint()][0] as Entity[T]; + return json[this.getEndPoint()][0] as WSEntity[T]; + } + + /** + * ?schema=blank&output_format=XML: returns a blank XML tree of the chosen resource. + */ + async getXMLBlank(): Promise { + const response: Response = await fetch( + this.getUrl(this.getEndPoint(), { + query: { + ws_key: this.getConfig().key, + schema: 'blank', + output_format: 'XML', + display: 'full', + }, + }), + { + method: 'GET', + mode: 'no-cors', + headers: this.getDefaultHeaders(), + }, + ); + + return response.text(); } /** @@ -282,7 +301,7 @@ export class BaseClient { * @param {Entity} entity - The blank entity * @param {Partial} entityData - Data for the entity to be created */ - fillFields(entity: Entity[T], entityData: Partial) { + fillFields(entity: WSEntity[T], entityData: Partial) { const requiredFieldsFound: string[] = []; for (const property of Object.keys(entityData)) { @@ -310,8 +329,8 @@ export class BaseClient { } removeReadOnlyFields( - entity: Partial, - entityData: Partial, + entity: Partial, + entityData: Partial, ) { for (const property in entity) { if ( @@ -338,7 +357,7 @@ export class BaseClient { isNotUpdatedField( property: string, - shopContentData: Partial, + shopContentData: Partial, ) { return !shopContentData[property]; }