Skip to content

Commit

Permalink
added .npmignore, fixes syntax issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Osman Goni Nahid committed Dec 13, 2023
1 parent d11b61d commit 33088cd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <osmangoni.se@gmail.com>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/resources/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 9 additions & 1 deletion src/resources/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> }) {
constructor(params: {
shopeeConfig: ShopeeConfig;
shopId: number;
accessToken: string;
onRefreshAccessToken?: () => Promise<string>;
}) {
this.shopId = params.shopId;
this.accessToken = params.accessToken;
this.shopeeRequest = ShopeeRequest.getAuthorizedInstance({
shopeeConfig: params.shopeeConfig,
shopId: params.shopId,
token: params.accessToken,
onRefreshAccessToken: params.onRefreshAccessToken,
Expand Down
31 changes: 9 additions & 22 deletions src/shopee.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,28 +23,24 @@ export class ShopeeRequest {
}

static getAuthorizedInstance({
shopeeConfig,
shopId,
token,
onRefreshAccessToken,
}: {
shopeeConfig: ShopeeConfig;
shopId: number;
token: string;
onRefreshAccessToken?: () => Promise<string>;
}): 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;
Expand All @@ -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,
) +
Expand Down
11 changes: 7 additions & 4 deletions src/shopee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -58,6 +60,7 @@ export class ShopeeService {

public initializeShop(shopId: number, accessToken: string, onRefreshAccessToken?: () => Promise<string>) {
this.shop = new Shop({
shopeeConfig: this.configs,
shopId,
accessToken,
onRefreshAccessToken,
Expand Down

0 comments on commit 33088cd

Please sign in to comment.