From 33088cd7785fd7f6857bb879466426d2cdc7fc50 Mon Sep 17 00:00:00 2001 From: Osman Goni Nahid Date: Wed, 13 Dec 2023 14:38:52 +0400 Subject: [PATCH] added .npmignore, fixes syntax issues --- .npmignore | 1 + package.json | 2 +- src/resources/auth.ts | 4 ++-- src/resources/shop.ts | 10 +++++++++- src/shopee.request.ts | 31 +++++++++---------------------- src/shopee.service.ts | 11 +++++++---- 6 files changed, 29 insertions(+), 30 deletions(-) create mode 100644 .npmignore diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +/node_modules diff --git a/package.json b/package.json index f3a1ea6..d608ecd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@osmangoninahid/nestjs-shopee", - "version": "1.0.0", + "version": "1.0.2", "description": "NestJS npm package for Shopee", "author": "Osman Goni Nahid ", "license": "MIT", diff --git a/src/resources/auth.ts b/src/resources/auth.ts index bd3d694..2fb7728 100644 --- a/src/resources/auth.ts +++ b/src/resources/auth.ts @@ -7,8 +7,8 @@ import { generateQueryParams } from '../util'; export class Auth { protected shopeeRequest: AxiosInstance; private readonly shopeeConfig: ShopeeConfig; - constructor(private config: ShopeeConfig) { - this.shopeeRequest = ShopeeRequest.getInstance(); + constructor(config: ShopeeConfig) { + this.shopeeRequest = ShopeeRequest.getInstance(config); this.shopeeConfig = config; } diff --git a/src/resources/shop.ts b/src/resources/shop.ts index 94b1761..8bbe4e0 100644 --- a/src/resources/shop.ts +++ b/src/resources/shop.ts @@ -5,18 +5,26 @@ import { ShopeeStoreUpdateDto } from '../dtos'; import { Order } from './order'; import { Product } from './product'; import { Logistic } from './logistic'; +import { ShopeeConfig } from '../shopee-config.interface'; export class Shop { protected shopeeRequest: AxiosInstance; + public shopeeConfig: ShopeeConfig; public shopId: number; public accessToken: string; private order: Order; private product: Product; private logistic: Logistic; - constructor(params: { shopId: number; accessToken: string; onRefreshAccessToken?: () => Promise }) { + constructor(params: { + shopeeConfig: ShopeeConfig; + shopId: number; + accessToken: string; + onRefreshAccessToken?: () => Promise; + }) { this.shopId = params.shopId; this.accessToken = params.accessToken; this.shopeeRequest = ShopeeRequest.getAuthorizedInstance({ + shopeeConfig: params.shopeeConfig, shopId: params.shopId, token: params.accessToken, onRefreshAccessToken: params.onRefreshAccessToken, diff --git a/src/shopee.request.ts b/src/shopee.request.ts index d728357..448cb9b 100644 --- a/src/shopee.request.ts +++ b/src/shopee.request.ts @@ -7,23 +7,14 @@ import { generateQueryParams } from './util'; @Injectable() export class ShopeeRequest { - private static shopeeConfig: ShopeeConfig; - constructor(@Inject(SHOPEE_CONFIG) private readonly configs: ShopeeConfig) { - ShopeeRequest.shopeeConfig = configs; - } - - static getInstance(): AxiosInstance { + static getInstance(shopeeConfig: ShopeeConfig): AxiosInstance { const instance = axios.create({ - baseURL: ShopeeRequest.shopeeConfig.host, + baseURL: shopeeConfig.host, }); instance.interceptors.request.use(function (config) { const parsed = queryString.parseUrl('/api/v2/' + config.url); config.url = - generateQueryParams( - parsed.url, - ShopeeRequest.shopeeConfig.partnerId.toString(), - ShopeeRequest.shopeeConfig.partnerKey, - ) + + generateQueryParams(parsed.url, shopeeConfig.partnerId.toString(), shopeeConfig.partnerKey) + '&' + queryString.stringify(parsed.query); return config; @@ -32,28 +23,24 @@ export class ShopeeRequest { } static getAuthorizedInstance({ + shopeeConfig, shopId, token, onRefreshAccessToken, }: { + shopeeConfig: ShopeeConfig; shopId: number; token: string; onRefreshAccessToken?: () => Promise; }): AxiosInstance { const instance = axios.create({ - baseURL: ShopeeRequest.shopeeConfig.host, + baseURL: shopeeConfig.host, }); instance.interceptors.request.use(async function (config) { const parsed = queryString.parseUrl('/api/v2/' + config.url); config.url = - generateQueryParams( - parsed.url, - ShopeeRequest.shopeeConfig.partnerId.toString(), - ShopeeRequest.shopeeConfig.partnerKey, - token, - shopId, - ) + + generateQueryParams(parsed.url, shopeeConfig.partnerId.toString(), shopeeConfig.partnerKey, token, shopId) + '&' + queryString.stringify(parsed.query); return config; @@ -79,8 +66,8 @@ export class ShopeeRequest { config.url = generateQueryParams( parsedUrl.url, - ShopeeRequest.shopeeConfig.partnerId.toString(), - ShopeeRequest.shopeeConfig.partnerKey, + shopeeConfig.partnerId.toString(), + shopeeConfig.partnerKey, newToken, shopId, ) + diff --git a/src/shopee.service.ts b/src/shopee.service.ts index 572bde0..0831c43 100644 --- a/src/shopee.service.ts +++ b/src/shopee.service.ts @@ -2,17 +2,19 @@ import { Inject, Injectable } from '@nestjs/common'; import { SHOPEE_CONFIG } from './constants'; import { ShopeeConfig } from './shopee-config.interface'; import { Auth } from './resources/auth'; -import { ShopeeAuthResponseDto } from './dtos/shopee-auth.response.dto'; +import { ShopeeAuthResponseDto } from './dtos'; import { Shop } from './resources/shop'; -import { ShopeeStoreUpdateDto } from './dtos/shopee-store.update.dto'; -import { ShopeeApiResponseDto } from './dtos/shopee-api.response.dto'; -import { ITEM_STATUS } from './dtos/item-status.enum'; +import { ShopeeStoreUpdateDto } from './dtos'; +import { ShopeeApiResponseDto } from './dtos'; +import { ITEM_STATUS } from './dtos'; @Injectable() export class ShopeeService { private readonly auth: Auth; private shop: Shop; + private readonly configs: ShopeeConfig; constructor(@Inject(SHOPEE_CONFIG) private readonly shopeeConfig: ShopeeConfig) { + this.configs = shopeeConfig; this.auth = new Auth(shopeeConfig); } @@ -58,6 +60,7 @@ export class ShopeeService { public initializeShop(shopId: number, accessToken: string, onRefreshAccessToken?: () => Promise) { this.shop = new Shop({ + shopeeConfig: this.configs, shopId, accessToken, onRefreshAccessToken,