Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,787 changes: 1,787 additions & 0 deletions dist/index.d.mts

Large diffs are not rendered by default.

1,787 changes: 1,787 additions & 0 deletions dist/index.d.ts

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions dist/index.global.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/index.global.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/index.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/index.mjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/index.mjs.map

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/converters/itemToBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ import { Item } from '@/types/api/response';
import { BundleProps } from '@/types/entities/Bundle';

export default <T extends object = BundleProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item);
const props = itemToObject<Record<string, unknown>>(item,{
active: 'boolean',
number: 'string',
name: 'string',
price: 'number',
currency: 'string',
productNumber: 'string',
licenseTemplateNumbers: 'string',
staleLicenseTemplateNumbers: 'string',
});

const { licenseTemplateNumbers } = props;
const { licenseTemplateNumbers, staleLicenseTemplateNumbers } = props;

if (licenseTemplateNumbers && typeof licenseTemplateNumbers === 'string') {
props.licenseTemplateNumbers = licenseTemplateNumbers.split(',');
}

if (staleLicenseTemplateNumbers && typeof staleLicenseTemplateNumbers === 'string') {
props.staleLicenseTemplateNumbers = staleLicenseTemplateNumbers.split(',');
}

return Bundle<T>(props as BundleProps<T>);
};
10 changes: 9 additions & 1 deletion src/converters/itemToCountry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ import Country from '@/entities/Country';
import { Item } from '@/types/api/response';
import { CountryProps } from '@/types/entities/Country';

export default (item?: Item) => Country(itemToObject<CountryProps>(item));
export default (item?: Item) =>
Country(
itemToObject<CountryProps>(item, {
code: 'string',
name: 'string',
vatPercent: 'number',
isEu: 'boolean',
}),
);
14 changes: 13 additions & 1 deletion src/converters/itemToLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ import { Item } from '@/types/api/response';
import { LicenseProps } from '@/types/entities/License';

export default <T extends object = LicenseProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item);
const props = itemToObject<Record<string, unknown>>(item, {
active: 'boolean',
number: 'string',
name: 'string',
price: 'number',
currency: 'string',
hidden: 'boolean',
licenseeNumber: 'string',
licenseTemplateNumber: 'string',
productModuleNumber: 'string',
startDate: 'string',
inUse: 'boolean',
});

const { startDate } = props;

Expand Down
17 changes: 16 additions & 1 deletion src/converters/itemToLicenseTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ import LicenseTemplate from '@/entities/LicenseTemplate';
import { Item } from '@/types/api/response';
import { LicenseTemplateProps } from '@/types/entities/LicenseTemplate';

export default <T extends object = LicenseTemplateProps>(item?: Item) => LicenseTemplate<T>(itemToObject<T>(item));
export default <T extends object = LicenseTemplateProps>(item?: Item) =>
LicenseTemplate<T>(
itemToObject<T>(item, {
active: 'boolean',
number: 'string',
name: 'string',
licenseType: 'string',
price: 'number',
currency: 'string',
automatic: 'boolean',
hidden: 'boolean',
hideLicenses: 'boolean',
productModuleNumber: 'string',
inUse: 'boolean',
}),
);
20 changes: 19 additions & 1 deletion src/converters/itemToLicensee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,22 @@ import Licensee from '@/entities/Licensee';
import { Item } from '@/types/api/response';
import { LicenseeProps } from '@/types/entities/Licensee';

export default <T extends object = LicenseeProps>(item?: Item) => Licensee<T>(itemToObject<T>(item));
export default <T extends object = LicenseeProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item, {
active: 'boolean',
number: 'string',
name: 'string',
markedForTransfer: 'boolean',
productNumber: 'string',
aliases: 'string',
inUse: 'boolean',
});

const { aliases } = props;

if (aliases && typeof aliases === 'string') {
props.aliases = aliases.split(',');
}

return Licensee<T>(props as LicenseeProps<T>);
};
10 changes: 9 additions & 1 deletion src/converters/itemToNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import { Item } from '@/types/api/response';
import { NotificationProps } from '@/types/entities/Notification';

export default <T extends object = NotificationProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item);
const props = itemToObject<Record<string, unknown>>(item, {
active: 'boolean',
number: 'string',
name: 'string',
protocol: 'string',
events: 'string',
payload: 'string',
endpoint: 'string',
});

const { events } = props;

Expand Down
28 changes: 14 additions & 14 deletions src/converters/itemToObject.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { Item, List } from '@/types/api/response';
import { CastType } from '@/types/utils/cast';
import cast from '@/utils/cast';

const cast = (value: string): unknown => {
try {
return JSON.parse(value);
} catch (e) {
return value;
}
};
export type ItemToObjectCastMap = Record<string, CastType>;

const extractProperties = (properties?: { name: string; value: string }[]) => {
const extractProperties = (properties?: { name: string; value: string }[], castMap: ItemToObjectCastMap = {}) => {
const result: Record<string, unknown> = {};
properties?.forEach(({ name, value }) => {
result[name] = cast(value);
result[name] = cast(value, castMap[name]);
});
return result;
};

const extractLists = (lists?: List[]) => {
const extractLists = (lists?: List[], castMap: ItemToObjectCastMap = {}) => {
const result: Record<string, unknown[]> = {};

lists?.forEach((list) => {
const { name } = list;
result[name] = result[name] || [];
result[name].push(itemToObject(list));
result[name].push(itemToObject(list, castMap));
});
return result;
};

const itemToObject = <T extends object = Record<string, unknown>>(item?: Item | List): T => {
return item ? ({ ...extractProperties(item.property), ...extractLists(item.list) } as T) : ({} as T);
};
const itemToObject =
<T extends object = Record<string, unknown>>(item?: Item | List, castMap: ItemToObjectCastMap = {}): T => {
return item
? ({...extractProperties(item.property, castMap), ...extractLists(item.list, castMap)} as T)
: ({} as T);
};


export default itemToObject;
8 changes: 7 additions & 1 deletion src/converters/itemToPaymentMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ import PaymentMethod from '@/entities/PaymentMethod';
import { Item } from '@/types/api/response';
import { PaymentMethodProps } from '@/types/entities/PaymentMethod';

export default <T extends object = PaymentMethodProps>(item?: Item) => PaymentMethod<T>(itemToObject<T>(item));
export default <T extends object = PaymentMethodProps>(item?: Item) =>
PaymentMethod<T>(
itemToObject<T>(item, {
active: 'boolean',
number: 'string',
}),
);
10 changes: 9 additions & 1 deletion src/converters/itemToProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import { ProductProps } from '@/types/entities/Product';
import { ProductDiscountEntity } from '@/types/entities/ProductDiscount';

export default <T extends object = ProductProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item);
const props = itemToObject<Record<string, unknown>>(item, {
number: 'string',
name: 'string',
version: 'string',
description: 'string',
licensingInfo: 'string',
licenseeAutoCreate: 'boolean',
inUse: 'boolean',
});

const discounts: ProductDiscountEntity[] | undefined = props.discount as ProductDiscountEntity[] | undefined;
delete props.discount;
Expand Down
12 changes: 11 additions & 1 deletion src/converters/itemToProductModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ import ProductModule from '@/entities/ProductModule';
import { Item } from '@/types/api/response';
import { ProductModuleProps } from '@/types/entities/ProductModule';

export default <T extends object = ProductModuleProps>(item?: Item) => ProductModule<T>(itemToObject<T>(item));
export default <T extends object = ProductModuleProps>(item?: Item) =>
ProductModule<T>(
itemToObject<T>(item, {
active: 'boolean',
number: 'string',
name: 'string',
licensingModel: 'string',
productNumber: 'string',
inUse: 'boolean',
}),
);
19 changes: 18 additions & 1 deletion src/converters/itemToToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ import { Item } from '@/types/api/response';
import { TokenProps } from '@/types/entities/Token';

export default <T extends object = TokenProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item);
const props = itemToObject<Record<string, unknown>>(item, {
active: 'boolean',
number: 'string',
expirationTime: 'string',
tokenType: 'string',
licenseeNumber: 'string',
action: 'string',
apiKeyRole: 'string',
bundleNumber: 'string',
bundlePrice: 'number',
productNumber: 'string',
predefinedShoppingItem: 'string',
successURL: 'string',
successURLTitle: 'string',
cancelURL: 'string',
cancelURLTitle: 'string',
shopURL: 'string',
});

const { expirationTime } = props;

Expand Down
23 changes: 18 additions & 5 deletions src/converters/itemToTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ import { Item } from '@/types/api/response';
import { TransactionProps } from '@/types/entities/Transaction';

export default <T extends object = TransactionProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item);
const props = itemToObject<Record<string, unknown>>(item, {
active: 'boolean',
number: 'string',
status: 'string',
source: 'string',
grandTotal: 'number',
discount: 'number',
currency: 'string',
datecreated: 'string',
dateclosed: 'string',
paymentMethod: 'string',
licenseTransactionJoins: 'json',
inUse: 'boolean',
});

const { datecreated: dateCreated, dateclosed: dateClosed } = props;

Expand All @@ -30,15 +43,15 @@ export default <T extends object = TransactionProps>(item?: Item) => {
delete props.dateclosed;
}

const licenseTransactionJoins: { licenseNumber: string; transactionNumber: string }[] | undefined =
props.licenseTransactionJoin as { licenseNumber: string; transactionNumber: string }[];
type LicenseTransactionJoins = { licenseNumber: string | number; transactionNumber: string | number }[] | undefined;
const licenseTransactionJoins: LicenseTransactionJoins = props.licenseTransactionJoin as LicenseTransactionJoins;

delete props.licenseTransactionJoin;

if (licenseTransactionJoins) {
props.licenseTransactionJoins = licenseTransactionJoins.map(({ transactionNumber, licenseNumber }) => {
const transaction = Transaction({ number: transactionNumber });
const license = License({ number: licenseNumber });
const transaction = Transaction({ number: String(transactionNumber) });
const license = License({ number: String(licenseNumber) });

return LicenseTransactionJoin(transaction, license);
});
Expand Down
8 changes: 4 additions & 4 deletions src/services/Service/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosRequestConfig, Method, AxiosRequestHeaders, AxiosResponse, AxiosError, AxiosInstance } from 'axios';
import { AxiosRequestConfig, Method, AxiosRequestHeaders, AxiosResponse, AxiosError } from 'axios';

// constants
import SecurityMode from '@/constants/SecurityMode';
Expand All @@ -8,7 +8,7 @@ import NlicError from '@/errors/NlicError';

// types
import type { NlicResponse } from '@/types/api/response';
import type { RequestConfig } from '@/types/services/Service';
import type { AxiosRequestClient, RequestConfig } from '@/types/services/Service';
import { ContextInstance } from '@/types/vo/Context';

// package.json
Expand Down Expand Up @@ -95,10 +95,10 @@ export default async (
throw new NlicError('Unknown security mode');
}

const instance: AxiosInstance = config?.axiosInstance || getAxiosInstance();
const instance: AxiosRequestClient = config?.axiosInstance || getAxiosInstance();

try {
const response: AxiosResponse<NlicResponse> = await instance(req);
const response: AxiosResponse<NlicResponse> = await instance.request(req);
const info = response.data.infos?.info || [];

setLastResponse(response);
Expand Down
2 changes: 1 addition & 1 deletion src/types/entities/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ProductProps<T extends object = object> = {
active?: boolean;
number?: string;
name?: string;
version?: string | number;
version?: string;
description?: string;
licensingInfo?: string;
licenseeAutoCreate?: boolean;
Expand Down
8 changes: 5 additions & 3 deletions src/types/services/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import type { AxiosInstance, AxiosResponse, Method } from 'axios';
import { Info, NlicResponse } from '@/types/api/response';
import type { ContextInstance } from '@/types/vo/Context';

export type AxiosRequestClient = Pick<AxiosInstance, 'request'>;

export interface RequestConfig {
onInfo?: (info: Info[]) => void;
onResponse?: (response: AxiosResponse) => void;
axiosInstance?: AxiosInstance;
axiosInstance?: AxiosRequestClient;
}

export interface IService {
setAxiosInstance(this: void, instance: AxiosInstance): void;
setAxiosInstance(this: void, instance: AxiosRequestClient): void;

getAxiosInstance(this: void): AxiosInstance;
getAxiosInstance(this: void): AxiosRequestClient;

// @deprecated use getResponse in RequestConfig
getLastHttpRequestInfo(this: void): AxiosResponse | null;
Expand Down
1 change: 1 addition & 0 deletions src/types/utils/cast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type CastType = 'auto' | 'string' | 'number' | 'boolean' | 'null' | 'json';
Loading
Loading