@salla.sa/twilight 2.0.217 → 2.0.219

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/@salla.sa/twilight.min.js +2 -2
  2. package/dist/@salla.sa/twilight.min.js.map +1 -1
  3. package/dist/cjs/index.js +13 -0
  4. package/dist/cjs/index.js.map +1 -0
  5. package/dist/cjs/main.js +2 -2
  6. package/dist/cjs/main.js.map +1 -1
  7. package/dist/esm/index.js +13 -0
  8. package/dist/esm/index.js.map +1 -0
  9. package/dist/esm/main.js +2 -2
  10. package/dist/esm/main.js.map +1 -1
  11. package/package.json +1 -1
  12. package/types/actions.d.ts +50 -0
  13. package/types/api/cart.d.ts +4 -6
  14. package/types/api/comment.d.ts +12 -0
  15. package/types/api/currency.d.ts +17 -0
  16. package/types/api/document.d.ts +3 -0
  17. package/types/api/feedback.d.ts +56 -0
  18. package/types/api/gift.d.ts +26 -0
  19. package/types/api/index.d.ts +103 -0
  20. package/types/api/offer.d.ts +43 -0
  21. package/types/api/product.d.ts +29 -0
  22. package/types/api/search.d.ts +22 -0
  23. package/types/api/twilight.d.ts +6 -0
  24. package/types/api/wishlist.d.ts +7 -0
  25. package/types/common.d.ts +42 -4
  26. package/types/event/comment.d.ts +7 -0
  27. package/types/event/currency.d.ts +9 -0
  28. package/types/event/document.d.ts +21 -0
  29. package/types/event/feedback.d.ts +18 -0
  30. package/types/event/gift.d.ts +7 -0
  31. package/types/event/index.d.ts +56 -0
  32. package/types/event/infiniteScroll.d.ts +12 -0
  33. package/types/event/offer.d.ts +9 -0
  34. package/types/event/product.d.ts +9 -0
  35. package/types/event/profile.d.ts +2 -5
  36. package/types/event/search.d.ts +7 -0
  37. package/types/event/twilight.d.ts +5 -0
  38. package/types/event/wishlist.d.ts +8 -0
  39. package/types/helpers/app-helpers.d.ts +31 -0
  40. package/types/helpers/salla-helpers.d.ts +17 -9
  41. package/types/index.d.ts +30 -89
  42. package/types/lib/salla-config.ts +43 -5
  43. package/types/lib/salla-cookie.d.ts +5 -0
  44. package/types/lib/salla-form.d.ts +3 -0
  45. package/types/lib/salla-lang.d.ts +14 -0
  46. package/types/lib/salla-notify.d.ts +14 -0
  47. package/types/lib/salla-storage.d.ts +13 -0
  48. package/types/tiwlight-config.d.ts +57 -57
  49. package/types/all.d.ts +0 -5
@@ -0,0 +1,5 @@
1
+ import {TwilightConfig} from "../tiwlight-config";
2
+
3
+ export default interface TwilightEvent {
4
+ initiated: (callback: (config: TwilightConfig) => void) => void;
5
+ }
@@ -0,0 +1,8 @@
1
+ import {RequestErrorEventWithData, TwilightResponse} from "../common";
2
+
3
+ export default interface WishlistEvent {
4
+ onAdded: (callback: (response: TwilightResponse, product_id: number) => void) => void;
5
+ onRemoved: (callback: (response: TwilightResponse, product_id: number) => void) => void;
6
+ onAdditionFailed: RequestErrorEventWithData</*product_id*/number>;
7
+ onRemovingFailed: RequestErrorEventWithData</*product_id*/number>;
8
+ }
@@ -0,0 +1,31 @@
1
+ import {Nullable} from "../common";
2
+
3
+ type ElementOrSelector = string | HTMLElement;
4
+ type EventTypes = 'click' | 'keyup' | 'input' | 'change' | string;
5
+ export default interface AppHelpers {
6
+ toggleClassIf: (selector: string, classes1: string | Array<string>, classes2: string | Array<string>, callback: (element: HTMLElement) => boolean) => AppHelpers;
7
+ toggleElementClassIf: (element: HTMLElement, classes1: string | Array<string>, classes2: string | Array<string>, callback: (element: HTMLElement) => boolean) => AppHelpers;
8
+
9
+ isValidEmail: (email: string) => boolean;
10
+
11
+ element: (selector: ElementOrSelector) => Nullable<HTMLElement>;
12
+ watchElement: (name: string, selector: ElementOrSelector) => AppHelpers;
13
+ watchElements: (elements: { [key: string]: [value: ElementOrSelector] }) => AppHelpers;
14
+ hideElement: (element: ElementOrSelector) => AppHelpers;
15
+ showElement: (element: ElementOrSelector) => AppHelpers;
16
+ //💡 you can pass one or multi classes: this.removeClass(element, 'class_1', 'class_2', ...)
17
+ removeClass: (element: ElementOrSelector, ...className: string[]) => AppHelpers;
18
+ addClass: (element: ElementOrSelector, ...className: string[]) => AppHelpers;
19
+ //💡instead of: `document.querySelectorAll('.test').forEach(element=>{...})`
20
+ // you can use: `this.all('.test', element=>{...})`
21
+ all: (element: ElementOrSelector, callback: (element: HTMLElement) => void) => AppHelpers;
22
+
23
+ //💡instead of: `document.querySelectorAll('.test').forEach(el => el.addEventListener('keyup', callback));`
24
+ // you can use: `this.on('.test', 'keyup', callback)`
25
+ // or can use: `this.onKeyUp('.test', callback)`
26
+ on: (eventType: EventTypes, element: ElementOrSelector, callback: EventListener, options?: AddEventListenerOptions) => AppHelpers;
27
+ onClick: (element: ElementOrSelector, callback: EventListener) => AppHelpers;
28
+ onKeyUp: (element: ElementOrSelector, callback: EventListener) => AppHelpers;
29
+ onEnter: (element: ElementOrSelector, callback: EventListener) => AppHelpers;
30
+ debounce: (callback: () => unknown) => (...data: Nullable<any>) => Promise<unknown>
31
+ }
@@ -1,21 +1,29 @@
1
1
  import {Price} from "../common";
2
2
 
3
- export interface SallaHelpers {
3
+ export type UrlHelper = (path: string) => string; //add any path to current url, ex: `salla.url('cart?anything=data')` output => `https://store_domain.com/cart?anything=data`
4
+ export default interface SallaHelpers {
4
5
  //Numbers helpers
5
6
  digitsOnly: (num: string | number) => number,
6
7
  inputDigitsOnly: (input: string | HTMLInputElement) => number,
7
- number: () => number,
8
+ number: (num: number) => number | string,
8
9
  money: (num: number | Price) => string,
9
10
 
10
11
  //Nested objects helpers
11
- setNested: () => number,
12
- getNested: () => number,
13
- inputData: () => number,
12
+ setNested: (object: Object, key: `${string}.${string}`, value: any) => Object,
13
+ getNested: (object: Object, key: `${string}.${string}`, default_?: any) => any,
14
+ /**
15
+ * @example
16
+ * 1- initial data: `let data={items:{total:100,...}};`
17
+ * 2- input: `<input name="items[total]" value="new-value">`
18
+ * 3- getData as Object: `salla.helpers.inputData('items[total]', 'new-value', data)`
19
+ * 4- output: `{name: items: value:{total:[100, 'new-value'],...}}`
20
+ */
21
+ inputData: (inputName: `${string}[${null | string | number}]`, inputValue: any, data?: Object) => { name: string, value: string | number | Object | string[] | number[] },
14
22
 
15
23
  //Url helpers
16
- url: () => string,
17
- addParamToUrl: () => string,
24
+ url: UrlHelper,
25
+ addParamToUrl: (key: string, value: string | number) => string, //`salla.helpers.addParamToUrl('lang','ar')` => `https://store_domain.com/cart?lang=ar`
18
26
 
19
- //other
20
- debounce: () => number,
27
+ //for advance usage:
28
+ debounce: (callback: Function, delay: number) => (...[]) => Promise<unknown>,
21
29
  }
package/types/index.d.ts CHANGED
@@ -1,99 +1,40 @@
1
- import CartApi from "./api/cart";
2
- import {SallaConfig} from "./lib/salla-config";
3
- import {Axios} from "axios";
4
- import {Price} from "./common";
5
- import {SallaHelpers} from "./helpers/salla-helpers";
6
- import CartEvent from "./event/cart";
7
- import CouponEvent from "./event/coupon";
8
- import CouponApi from "./api/coupon";
9
- import AuthApi from "./api/auth";
10
- import AuthEvent from "./event/auth";
11
- import OrderApi from "./api/order";
12
- import OrderEvent from "./event/order";
13
- import ProfileApi from "./api/profile";
14
- import ProfileEvent from "./event/profile";
1
+ import SallaApi, {ApiActionName} from './api';
2
+ import SallaEvent from './event';
3
+ import SallaActions from "./actions";
15
4
 
16
- export * from "./all";
5
+ import SallaConfig from "./lib/salla-config";
6
+ import {Price} from "./common";
7
+ import SallaHelpers, {UrlHelper} from "./helpers/salla-helpers";
8
+ import AppHelpers from "./helpers/app-helpers";
9
+ import SallaNotify, {Logger} from "./lib/salla-notify";
10
+ import SallaStorage from "./lib/salla-storage";
11
+ import SallaLang from "./lib/salla-lang";
12
+ import SallaCookie from "./lib/salla-cookie";
13
+ import SallaForm from "./lib/salla-form";
17
14
 
18
- export default interface Salla {
19
- notify: string;
20
- api: Axios | {
21
- cart: CartApi,
22
- auth: AuthApi;
23
- gift: any;
24
- order: OrderApi;
25
- offer: any;
26
- search: any;
27
- coupon: CouponApi;
28
- comment: any;
29
- loyalty: any;
30
- product: any;
31
- profile: ProfileApi;
32
- currency: any;
33
- document: any;
34
- feedback: any;
35
- twilight: any;
36
- wishlist: any;
37
- getHeaders: () => {
38
- "Accept": string | 'application/json, text/plain, */*',
39
- "X-Requested-With": 'XMLHttpRequest',
40
- "S-SOURCE": 'twilight',
41
- "Store-Identifier"?: number,
42
- "currency"?: string | 'SAR',
43
- "accept-language"?: string | 'ar',
44
- "Authorization"?: string
45
- }
46
- };
47
- event: {
48
- auth: AuthEvent,
49
- cart: CartEvent,
50
- gift: any;
51
- order: OrderEvent;
52
- offer: any;
53
- search: any;
54
- coupon: CouponEvent;
55
- comment: any;
56
- loyalty: any;
57
- product: any;
58
- profile: ProfileEvent;
59
- currency: any;
60
- document: any;
61
- feedback: any;
62
- twilight: any;
63
- wishlist: any;
15
+ export default interface Salla extends SallaActions {
16
+ api: SallaApi;
17
+ event: SallaEvent;
64
18
 
65
- };
66
- infiniteScroll: any;
67
- helpers: SallaHelpers;
68
19
  config: SallaConfig;
69
- storage: any;
70
- form: any;
20
+ storage: SallaStorage;
21
+ cookie: SallaCookie;
22
+ lang: SallaLang;
71
23
 
72
- //Aliases
73
- cart: CartApi & { api: CartApi, event: CartEvent };
74
- auth: AuthApi & { api: AuthApi, event: AuthEvent };
75
- gift: { api: any, event: any };
76
- order: OrderApi & { api: OrderApi, event: OrderEvent };
77
- offer: { api: any, event: any };
78
- search: { api: any, event: any };
79
- coupon: CouponApi & { api: CouponApi, event: CouponEvent };
80
- comment: { api: any, event: any };
81
- loyalty: { api: any, event: any };
82
- product: { api: any, event: any };
83
- profile: ProfileApi & { api: ProfileApi, event: ProfileEvent };
84
- currency: { api: any, event: any };
85
- document: { api: any, event: any };
86
- feedback: { api: any, event: any };
87
- twilight: { api: any, event: any };
88
- wishlist: { api: any, event: any };
24
+ notify: SallaNotify;
25
+ log: Logger;
26
+ success: (message: string, data?: any) => void;
27
+ error: (message: string, data?: any) => void;
89
28
 
90
- log: (message: string | any) => void;
91
- success: (message: string) => void;
92
- error: (message: string) => void;
93
- lang: any;
94
- onReady: any; //don't relay on it because Salla may not existed yet
95
- AppHelpers: any;
29
+ helpers: SallaHelpers;
30
+ AppHelpers: AppHelpers;
31
+ form: SallaForm;
96
32
  money: (money: number | Price) => string;
33
+ url: UrlHelper;
34
+
35
+ onReady: (callback: (event: Event) => unknown) => void; //don't relay on it because Salla may not existed yet
36
+ init: (config: SallaConfig) => void; // an alias for `salla.twilight.init(config)`
37
+ call: (action: ApiActionName | string) => any;
97
38
  }
98
39
 
99
40
  declare global {
@@ -1,12 +1,50 @@
1
- import {TwilightConfig} from "../tiwlight-config";
2
- import {Price} from "../common";
1
+ import {Language, TwilightConfig} from "../tiwlight-config";
2
+ import {Currency, Nullable, Price} from "../common";
3
3
 
4
- export interface SallaConfig {
4
+ export type ConfigKey =
5
+ '_token'
6
+ | 'maintenance'
7
+ | 'debug'
8
+ | 'events'
9
+ | 'store.url'
10
+ | 'store.id'
11
+ | 'store.logo'
12
+ | 'store.name'
13
+ | 'store.api'
14
+ | 'user.type'
15
+ | 'user.email'
16
+ | 'user.mobile'
17
+ | 'user.language_code'
18
+ | 'user.currency_code'
19
+ | 'user.country_code'
20
+ | 'theme.name'
21
+ | 'theme.mode'
22
+ | 'theme.translations_hash'
23
+ | 'theme.color'
24
+ | 'theme.color.primary'
25
+ | 'theme.color.text'
26
+ | 'theme.color.is_dark'
27
+ | 'theme.color.reverse_primary'
28
+ | 'theme.color.reverse_text'
29
+ | 'languages.ar'
30
+ | 'languages.en'
31
+ | '`languages.${string}`'
32
+ | 'currencies.SAR'
33
+ | 'currencies.USD'
34
+ | '`currencies.${string}`'
35
+ | 'page.title'
36
+ | 'page.slug'
37
+ | 'page.id';
38
+ export default interface SallaConfig {
5
39
  merge: (config: TwilightConfig) => SallaConfig;
6
- set: (key: string, value: string) => SallaConfig;
7
- get: (key: string, default_?: any) => any;
40
+ set: (key: ConfigKey | string, value: string) => SallaConfig;
41
+ get: (key: ConfigKey | string, default_?: any) => any;
8
42
  isRTL: () => boolean;
9
43
  money: (money: number | Price) => string;
10
44
  isUser: () => boolean;
11
45
  isGuest: () => boolean;
46
+ languageCode: () => string | 'ar';
47
+ currency: (code: Nullable<string | 'SAR'>) => Currency;
48
+ currencies: () => Promise<{ SAR: Currency, [code: string]: Currency }>;
49
+ languages: () => Promise<{ ar: Language, [code: string]: Language }>;
12
50
  }
@@ -0,0 +1,5 @@
1
+ export default interface SallaCookie {
2
+ get: (key: string) => string;
3
+ set: (key: string, value: string | number | Array<string | number> | '', days?: number) => SallaCookie;
4
+ remove: (key: string) => SallaCookie;
5
+ }
@@ -0,0 +1,3 @@
1
+ export default interface SallaForm {
2
+ submit: () => Promise<any>;
3
+ }
@@ -0,0 +1,14 @@
1
+ import Lang from 'lang.js'
2
+
3
+ type Replacements = { [key: string]: string }
4
+
5
+ /**
6
+ * @Example
7
+ * salla.lang.get('pages.cart.free_shipping_alert',{amount:salla.money(100)})
8
+ */
9
+ export default interface SallaLang extends Lang {
10
+ translationsLoaded: boolean;
11
+ onLoaded: (callback: Function) => void;
12
+ get: (key: string, replacements?: Replacements, locale?: string | 'ar') => string;
13
+ set: (key: string, message: string) => SallaLang;
14
+ }
@@ -0,0 +1,14 @@
1
+ export type NotifyType = 'error' | 'success' | 'info' | string;
2
+ export type Notifier = (message: string, type: NotifyType, data?: object | any) => void | unknown;
3
+ export type Logger = (...data: string[]) => void | unknown;
4
+ export default interface SallaNotify {
5
+ fire: (message: string, type: NotifyType, data?: object | any) => Notifier; //default is just `alert(message)
6
+ setNotifier: (callback: Notifier) => void;// handy to override notifier used by salla.notify.fire(salla.success, salla.error, salla.info)
7
+ log: Logger; //default is just `console.log(...data)`
8
+ setLogger: (callback: Logger) => void;// handy to override logger used by salla.log()
9
+ error: (message: string, data?: object | any) => Notifier; //an alias for fire(message, salla.notify.types.error, data)
10
+ success: (message: string, data?: object | any) => Notifier; //an alias for fire(message, salla.notify.types.success, data)
11
+ info: (message: string, data?: object | any) => Notifier; //an alias for fire(message, salla.notify.types.info, data)
12
+ sallaInitiated;
13
+ types: { error: 'error', success: 'success', info: 'info' }; //handy to be overridden, to be passed via salla.error, salla.info, salla.success second argument when calling salla.notify.fire(message, /*{type}*/, data)
14
+ }
@@ -0,0 +1,13 @@
1
+ import localStore from 'store';
2
+
3
+ /**
4
+ * 💡 Notice that you can use dotted key for setting or getting values, ex:
5
+ * - salla.storage.set('cart.id',1)
6
+ * - `salla.storage.get('cart.id')` or `salla.storage.get('cart')?.id`
7
+ */
8
+ export default interface SallaStorage {
9
+ store: localStore;
10
+ set: (key: string, value: any) => any;
11
+ get: (key: string, defaultValue?: any) => any | undefined;
12
+ remove: (key: string) => void;
13
+ }
@@ -1,25 +1,4 @@
1
- export interface Store {
2
- id: number;
3
- logo: string;
4
- name: string;
5
- url: string;
6
- api: string;
7
- }
8
-
9
- export interface User {
10
- type: string;
11
- email?: any;
12
- mobile?: any;
13
- language: string;
14
- currency: string;
15
- country_code: string;
16
- }
17
-
18
- export interface Theme {
19
- name: string;
20
- mode: string;
21
- translations_hash: string;
22
- }
1
+ import {Currency} from "./common";
23
2
 
24
3
  export interface Language {
25
4
  name: string;
@@ -29,41 +8,62 @@ export interface Language {
29
8
  country_code: string;
30
9
  }
31
10
 
32
- export interface Languages {
33
- ar: Language;
34
-
35
- [key: string]: Language;
36
- }
37
-
38
- export interface Currency {
39
- code: string;
40
- name: string;
41
- symbol: string;
42
- amount: number;
43
- country_code: string;
44
- }
45
-
46
- export interface Currencies {
47
- SAR: Currency;
48
-
49
- [key: string]: Currency;
50
- }
51
-
52
- export interface Page {
53
- title: string;
54
- slug: string;
55
- id?: number;
56
- }
11
+ type Installment = 'tabby_installment' | 'tamara_installment' | 'spotii_pay';
12
+ type PaymentName = Installment | 'mada' | 'credit_card' | 'paypal' | 'bank' | 'stc_pay' | 'apple_pay' | 'knet';
57
13
 
58
14
  export interface TwilightConfig {
59
- _token: string;
60
- store: Store;
61
- maintenance: boolean;
62
- debug: boolean;
63
- user: User;
64
- theme: Theme;
65
- languages: Languages;
66
- currencies: Currencies;
67
- page: Page;
68
- events: { [event_name: string]: any };
15
+ _token?: string;
16
+ maintenance?: boolean;
17
+ debug?: boolean;
18
+ events?: { [event_name: string]: any };
19
+ store: {
20
+ url: string;
21
+ id?: number;
22
+ logo?: string;
23
+ name?: string;
24
+ api?: string;
25
+ settings?: {
26
+ auth: {
27
+ email_allowed: boolean;
28
+ mobile_allowed: boolean;
29
+ is_email_required: boolean;
30
+ };
31
+ arabic_numbers_enabled: boolean;
32
+ payments: PaymentName[];
33
+ installments: Installment[];
34
+ };
35
+ };
36
+ user?: {
37
+ type?: 'user' | 'guest';
38
+ email?: any;
39
+ mobile?: any;
40
+ language_code?: string;
41
+ currency_code?: string;
42
+ country_code?: string;
43
+ };
44
+ theme?: {
45
+ name?: string;
46
+ mode?: string;
47
+ translations_hash?: string;
48
+ color?: {
49
+ primary: string | '#5cd5c4';
50
+ text: string | '#ffffff';
51
+ is_dark: boolean;
52
+ reverse_primary: string | '#5cd5c4';
53
+ reverse_text: string | '#005544';
54
+ }
55
+ };
56
+ languages?: {
57
+ ar?: Language;
58
+ [key: string]: Language;
59
+ };
60
+ currencies?: {
61
+ SAR?: Currency;
62
+ [key: string]: Currency;
63
+ };
64
+ page?: {
65
+ title?: string;
66
+ slug?: string;
67
+ id?: number;//available in product.single, customer.orders.single, cart, thank-you, page-single
68
+ };
69
69
  }
package/types/all.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './api/cart/cart-details-response';
2
- export * from './api/cart';
3
- export * from './api/coupon';
4
- export * from './api/cart/cart-status-response';
5
- export * from './api/order/re-order-response';