Skip to content

Commit

Permalink
feat(type): new types (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd authored Jan 8, 2023
2 parents 2ec02dc + 614037d commit 3e72c3f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 25 deletions.
3 changes: 3 additions & 0 deletions core/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
"@alwatr/logger": "^0.27.0",
"@alwatr/signal": "^0.27.0",
"tslib": "^2.4.1"
},
"devDependencies": {
"@alwatr/type": "^0.27.0"
}
}
2 changes: 2 additions & 0 deletions core/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {SignalInterface} from '@alwatr/signal';

import type {I18nConfig, L10Resource, Locale} from './type.js';

export * from './type.js';

const logger = createLogger('alwatr/i18n');

globalAlwatr.registeredList.push({
Expand Down
27 changes: 4 additions & 23 deletions core/i18n/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {L10Resource, Locale} from '@alwatr/type';

export {L10Resource, Locale};

declare global {
interface AlwatrSignals {
'locale-change': Locale;
Expand All @@ -9,29 +13,6 @@ declare global {
}
}

export type LocalCode = `${Lowercase<string>}-${Uppercase<string>}`;

export type L10Resource = Record<string, string> & {
_code: LocalCode;
};

export type Locale = {
/**
* fa-IR, en-US, ...
*/
code: LocalCode;

/**
* fa, en, ...
*/
language: Lowercase<string>;

/**
* ltr, rtl
*/
direction: 'rtl' | 'ltr';
};

export type I18nConfig = {
/**
* Automatically fetch the localization resource from `resourcePath/localCode.json`.
Expand Down
1 change: 1 addition & 0 deletions core/i18n/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"include": ["src/**/*.ts"],
"exclude": [],
"references": [
{"path": "../type"},
{"path": "../logger"},
{"path": "../signal"},
{"path": "../fetch"}
Expand Down
82 changes: 82 additions & 0 deletions core/type/src/customer-order-management.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {i18nString} from './i18n.js';
import {Photo} from './photo.js';

import type {AlwatrDocumentObject} from './storage.js';

export type Product = AlwatrDocumentObject & {
/**
* Product global ID
*
* like product unique code
*/
id: string;

/**
* Product title
*/
title: i18nString;

/**
* Product image
*/
image: Photo;
};

export type ProductPrice = AlwatrDocumentObject & {
/**
* Displayed price before discount
*/
price: number;

/**
* Final price after any discount
*/
finalPrice: number;
}

export type Order = AlwatrDocumentObject & {
/**
* Order unique code
*
* customerId-orderId
*/
id: `${number}-${number}`;

/**
* Products list with price and qty
*/
itemList: Array<OrderItem>;

/**
* Delivery info
*/
delivery: OrderDelivery;

discount: number;
discountType: typeof discountTypeCS[number];

totalPrice: number;
shippingPrice: number;
finalPrice: number;
};

// FIXME: name and values
export const shipmentTypeCS = ['x', 'y'] as const;
export const carTypeCS = ['x', 'y'] as const;
export const timePeriodCS = ['1-2w', '2-3w', '3-4w'] as const;
export const discountTypeCS = ['number', 'percent'] as const;

export type OrderDelivery = {
recipientName: string;
recipientNationalCode: string;
address: string;
shipmentType: typeof shipmentTypeCS[number];
carType: typeof carTypeCS[number];
timePeriod: typeof timePeriodCS[number];
}

export type OrderItem = {
productId: string;
price: ProductPrice;
qty: number;
};
29 changes: 29 additions & 0 deletions core/type/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export type LocalCode = `${Lowercase<string>}-${Uppercase<string>}`;

export type L10Resource = Record<string, string> & {
_code: LocalCode;
};

export type Locale = {
/**
* fa-IR, en-US, ...
*/
code: LocalCode;

/**
* fa, en, ...
*/
language: Lowercase<string>;

/**
* ltr, rtl
*/
direction: 'rtl' | 'ltr';
};

/**
* Multi language string
*
* {fa: 'سلام', en: 'hello'}
*/
export type i18nString = Record<Lowercase<string>, string>;
1 change: 1 addition & 0 deletions core/type/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './chat.js';
export * from './service-response.js';
export * from './storage.js';
export * from './global.js';
export * from './i18n.js';
export * from './type-helper.js';

Alwatr.registeredList.push({
Expand Down
15 changes: 15 additions & 0 deletions core/type/src/photo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {AlwatrDocumentObject} from './storage.js';

export type Photo = AlwatrDocumentObject & {
/**
* Primary Photo ID
*
* like full relative path (include extension) to image CDN (temporary)
*/
id: string; // path/file-name.png

/**
* Photo extra meta information for future maintenances
*/
meta: Record<string, string | number>; // meta: {order: 1233, customer: 1334}
}
4 changes: 2 additions & 2 deletions core/type/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export type AlwatrDocumentObject = {
};
};

export type AlwatrDocumentMeta = {
export type AlwatrStorageMeta = {
formatVersion: number;
reversion: number;
lastUpdated: number;
lastAutoId: number;
};

export type AlwatrDocumentStorage<T extends AlwatrDocumentObject> = Omit<
AlwatrServiceResponseSuccessWithMeta<Record<string, T>, AlwatrDocumentMeta>,
AlwatrServiceResponseSuccessWithMeta<Record<string, T>, AlwatrStorageMeta>,
'statusCode' | 'errorCode'
>;
21 changes: 21 additions & 0 deletions core/type/src/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type {AlwatrDocumentObject} from './storage.js';

export const genderCS = ['male', 'female'] as const;
export type Gender = typeof genderCS[number];

export type User = AlwatrDocumentObject & {
/**
* User global unique id (verifiable)
*/
id: string;

/**
* User full name
*/
fullName: string;

gender?: Gender;

email?: string;
phoneNumber?: string;
};

0 comments on commit 3e72c3f

Please sign in to comment.