@geins/core 0.3.0-canary
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.
- package/.turbo/turbo-build.log +7 -0
- package/CHANGELOG.md +13 -0
- package/README.md +1 -0
- package/__tests__/Channel.test.ts +34 -0
- package/__tests__/EventService.test.ts +32 -0
- package/__tests__/GeinsCore.test.ts +69 -0
- package/__tests__/Graphql.test.ts +43 -0
- package/dist/api-client/endpointClient.d.ts +8 -0
- package/dist/api-client/index.d.ts +4 -0
- package/dist/api-client/managementApiClient.d.ts +10 -0
- package/dist/api-client/merchantApiClient.d.ts +46 -0
- package/dist/base/baseApiService.d.ts +14 -0
- package/dist/base/basePackage.d.ts +12 -0
- package/dist/base/baseStore.d.ts +14 -0
- package/dist/base/index.d.ts +3 -0
- package/dist/constants/cookies.d.ts +11 -0
- package/dist/constants/endpoints.d.ts +14 -0
- package/dist/constants/headerNames.d.ts +3 -0
- package/dist/constants/index.d.ts +3 -0
- package/dist/geinsCore.d.ts +88 -0
- package/dist/graphql/index.d.ts +1 -0
- package/dist/graphql/queries.d.ts +5 -0
- package/dist/index.cjs +21783 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.esm.js +21749 -0
- package/dist/logic/channel.d.ts +17 -0
- package/dist/logic/index.d.ts +1 -0
- package/dist/parsers/channelParser.d.ts +8 -0
- package/dist/services/channelService.d.ts +8 -0
- package/dist/services/channelsService.d.ts +8 -0
- package/dist/services/cookieService.d.ts +88 -0
- package/dist/services/eventService.d.ts +51 -0
- package/dist/services/graphQLService.d.ts +23 -0
- package/dist/services/index.d.ts +7 -0
- package/dist/services/logService.d.ts +19 -0
- package/dist/services/routingService.d.ts +74 -0
- package/dist/stores/channelStore.d.ts +11 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/routingStore.d.ts +32 -0
- package/dist/utils/helpers.d.ts +21 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/simpleCache.d.ts +9 -0
- package/eslint.config.js +3 -0
- package/jest.config.js +8 -0
- package/package.json +32 -0
- package/rollup.config.js +26 -0
- package/src/api-client/endpointClient.ts +52 -0
- package/src/api-client/index.ts +4 -0
- package/src/api-client/managementApiClient.ts +41 -0
- package/src/api-client/merchantApiClient.ts +153 -0
- package/src/base/baseApiService.ts +96 -0
- package/src/base/basePackage.ts +39 -0
- package/src/base/baseStore.ts +21 -0
- package/src/base/index.ts +3 -0
- package/src/constants/cookies.ts +12 -0
- package/src/constants/endpoints.ts +27 -0
- package/src/constants/headerNames.ts +3 -0
- package/src/constants/index.ts +3 -0
- package/src/geinsCore.ts +214 -0
- package/src/graphql/channel.gql +16 -0
- package/src/graphql/channels.gql +16 -0
- package/src/graphql/fragments/channelFields.gql +8 -0
- package/src/graphql/fragments/countryFields.gql +4 -0
- package/src/graphql/fragments/currencyFields.gql +4 -0
- package/src/graphql/fragments/languageFields.gql +5 -0
- package/src/graphql/fragments/marketFields.gql +19 -0
- package/src/graphql/graphql.d.ts +4 -0
- package/src/graphql/index.ts +1 -0
- package/src/graphql/queries.ts +9 -0
- package/src/index.ts +10 -0
- package/src/logic/channel.ts +102 -0
- package/src/logic/index.ts +1 -0
- package/src/parsers/channelParser.ts +91 -0
- package/src/services/channelService.ts +33 -0
- package/src/services/channelsService.ts +35 -0
- package/src/services/cookieService.ts +168 -0
- package/src/services/eventService.ts +89 -0
- package/src/services/graphQLService.ts +55 -0
- package/src/services/index.ts +7 -0
- package/src/services/logService.ts +434 -0
- package/src/services/routingService.ts +196 -0
- package/src/stores/channelStore.ts +34 -0
- package/src/stores/index.ts +2 -0
- package/src/stores/routingStore.ts +56 -0
- package/src/utils/helpers.ts +39 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/simpleCache.ts +27 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { CookieService } from '../services/cookieService';
|
|
2
|
+
import { AUTH_COOKIES } from '../constants';
|
|
3
|
+
import {
|
|
4
|
+
ApolloClient,
|
|
5
|
+
InMemoryCache,
|
|
6
|
+
NormalizedCacheObject,
|
|
7
|
+
DocumentNode,
|
|
8
|
+
FetchPolicy,
|
|
9
|
+
ApolloQueryResult,
|
|
10
|
+
FetchResult,
|
|
11
|
+
OperationVariables,
|
|
12
|
+
} from '@apollo/client/core';
|
|
13
|
+
|
|
14
|
+
export enum FetchPolicyOptions {
|
|
15
|
+
CACHE_FIRST = 'cache-first',
|
|
16
|
+
NETWORK_ONLY = 'network-only',
|
|
17
|
+
CACHE_ONLY = 'cache-only',
|
|
18
|
+
NO_CACHE = 'no-cache',
|
|
19
|
+
STANDBY = 'standby',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export enum OperationType {
|
|
23
|
+
QUERY = 'query',
|
|
24
|
+
MUTATION = 'mutation',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface RequestOptions {
|
|
28
|
+
fetchPolicy?: FetchPolicy;
|
|
29
|
+
pollInterval?: number;
|
|
30
|
+
context?: any;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MerchantApiClientOptions {
|
|
35
|
+
apiUrl: string;
|
|
36
|
+
apiKey: string;
|
|
37
|
+
fetchPolicy?: FetchPolicy;
|
|
38
|
+
userToken?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface GraphQLQueryOptions {
|
|
42
|
+
query?: any;
|
|
43
|
+
variables?: any;
|
|
44
|
+
requestOptions?: RequestOptions;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class MerchantApiClient {
|
|
48
|
+
private _cookieService: CookieService | undefined;
|
|
49
|
+
private _apolloClient: ApolloClient<NormalizedCacheObject>;
|
|
50
|
+
private _userToken?: string;
|
|
51
|
+
fetchPolicy: FetchPolicy = FetchPolicyOptions.CACHE_FIRST;
|
|
52
|
+
pollInterval: number = 0;
|
|
53
|
+
|
|
54
|
+
constructor(options: MerchantApiClientOptions) {
|
|
55
|
+
const { apiUrl, apiKey, userToken, fetchPolicy } = options;
|
|
56
|
+
this._apolloClient = this.createClient(apiUrl, apiKey);
|
|
57
|
+
this._cookieService = new CookieService();
|
|
58
|
+
if (userToken) {
|
|
59
|
+
this._userToken = userToken;
|
|
60
|
+
}
|
|
61
|
+
if (fetchPolicy) {
|
|
62
|
+
this.fetchPolicy = fetchPolicy;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public updateToken(newToken?: string) {
|
|
67
|
+
this._userToken = newToken;
|
|
68
|
+
}
|
|
69
|
+
public get userToken() {
|
|
70
|
+
return this._userToken;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
createClient(apiUrl: string, apiKey: string) {
|
|
74
|
+
return new ApolloClient({
|
|
75
|
+
uri: apiUrl,
|
|
76
|
+
cache: new InMemoryCache(),
|
|
77
|
+
headers: {
|
|
78
|
+
Accept: 'application/json',
|
|
79
|
+
'x-apikey': apiKey,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getClient(): ApolloClient<NormalizedCacheObject> | undefined {
|
|
85
|
+
return this._apolloClient;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public clearCacheAndRefetchQueries() {
|
|
89
|
+
this._apolloClient.resetStore();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private getFetchPolicy(operationType: OperationType, selectedFetchPolicy: FetchPolicy | undefined) {
|
|
93
|
+
if (operationType === OperationType.QUERY) {
|
|
94
|
+
if (selectedFetchPolicy) {
|
|
95
|
+
return selectedFetchPolicy;
|
|
96
|
+
} else {
|
|
97
|
+
return this.fetchPolicy;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (
|
|
102
|
+
selectedFetchPolicy === FetchPolicyOptions.NO_CACHE ||
|
|
103
|
+
selectedFetchPolicy === FetchPolicyOptions.NETWORK_ONLY
|
|
104
|
+
) {
|
|
105
|
+
return selectedFetchPolicy;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return FetchPolicyOptions.NETWORK_ONLY;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private getOperationObject(
|
|
112
|
+
operationType: OperationType,
|
|
113
|
+
document: DocumentNode,
|
|
114
|
+
variables: OperationVariables = {},
|
|
115
|
+
options: RequestOptions = {},
|
|
116
|
+
) {
|
|
117
|
+
const token = this._userToken || this._cookieService?.get(AUTH_COOKIES.USER_AUTH);
|
|
118
|
+
// remove typename from variables
|
|
119
|
+
|
|
120
|
+
const operationObj: any = {
|
|
121
|
+
[operationType]: document,
|
|
122
|
+
variables,
|
|
123
|
+
fetchPolicy: this.getFetchPolicy(operationType, options.fetchPolicy),
|
|
124
|
+
pollInterval: options.pollInterval || this.pollInterval,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
if (token) {
|
|
128
|
+
operationObj.context = {
|
|
129
|
+
headers: {
|
|
130
|
+
Authorization: `Bearer ${token}`,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return operationObj;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async runQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(
|
|
139
|
+
options: GraphQLQueryOptions,
|
|
140
|
+
): Promise<ApolloQueryResult<TData>> {
|
|
141
|
+
const { query, variables, requestOptions } = options;
|
|
142
|
+
const q = this.getOperationObject(OperationType.QUERY, query, variables, requestOptions);
|
|
143
|
+
return this._apolloClient.query<TData, TVariables>(q);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async runMutation<TData = any, TVariables extends OperationVariables = OperationVariables>(
|
|
147
|
+
options: GraphQLQueryOptions,
|
|
148
|
+
): Promise<FetchResult<TData>> {
|
|
149
|
+
const { query, variables, requestOptions } = options;
|
|
150
|
+
const q = this.getOperationObject(OperationType.MUTATION, query, variables, requestOptions);
|
|
151
|
+
return this._apolloClient.mutate<TData, TVariables>(q);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { GeinsSettings } from '@geins/types';
|
|
2
|
+
import { GraphQLQueryOptions } from '../api-client/merchantApiClient';
|
|
3
|
+
|
|
4
|
+
export abstract class BaseApiService {
|
|
5
|
+
protected _apiClient: any;
|
|
6
|
+
protected _geinsSettings: GeinsSettings;
|
|
7
|
+
protected _channelId: string;
|
|
8
|
+
|
|
9
|
+
constructor(apiClient: any, geinsSettings: GeinsSettings) {
|
|
10
|
+
this._apiClient = apiClient;
|
|
11
|
+
|
|
12
|
+
if (!geinsSettings.channel) {
|
|
13
|
+
throw new Error('Channel is required');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
this._geinsSettings = geinsSettings;
|
|
17
|
+
this._channelId = `${geinsSettings.channel}|${geinsSettings.tld}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
protected createVariables(vars: any) {
|
|
21
|
+
const variables = { ...vars };
|
|
22
|
+
|
|
23
|
+
if (!variables.languageId) {
|
|
24
|
+
if (!this._geinsSettings.locale) {
|
|
25
|
+
throw new Error('Language is required');
|
|
26
|
+
}
|
|
27
|
+
variables.languageId = this._geinsSettings.locale;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!variables.marketId) {
|
|
31
|
+
if (!this._geinsSettings.market) {
|
|
32
|
+
throw new Error('Market is required');
|
|
33
|
+
}
|
|
34
|
+
variables.marketId = this._geinsSettings.market;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!variables.channelId) {
|
|
38
|
+
variables.channelId = this._channelId;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return variables;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected async runQuery(options: GraphQLQueryOptions) {
|
|
45
|
+
if (!this._apiClient) {
|
|
46
|
+
throw new Error('Merchant API Client is not set');
|
|
47
|
+
}
|
|
48
|
+
return this._apiClient().runQuery(options);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected async runQueryParsed<T>(options: GraphQLQueryOptions): Promise<T> {
|
|
52
|
+
const result = this.cleanObject(await this.runQuery(options));
|
|
53
|
+
const parsedResult = this.parseResult(result);
|
|
54
|
+
return parsedResult as T;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected async runMutation(options: GraphQLQueryOptions) {
|
|
58
|
+
if (!this._apiClient) {
|
|
59
|
+
throw new Error('Merchant API Client is not set');
|
|
60
|
+
}
|
|
61
|
+
return this._apiClient().runMutation(options);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected parseResult(result: any): any {
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
protected cleanObject<T extends Record<string, any>>(obj: T): Partial<T> {
|
|
69
|
+
const cleanedObj = Object.entries(obj).reduce(
|
|
70
|
+
(acc, [key, value]) => {
|
|
71
|
+
// Skip __typename properties
|
|
72
|
+
if (key.startsWith('__typename')) {
|
|
73
|
+
return acc;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Check if the value is an array, and clean each object inside it
|
|
77
|
+
if (Array.isArray(value)) {
|
|
78
|
+
acc[key] = value.map(item => (item && typeof item === 'object' ? this.cleanObject(item) : item));
|
|
79
|
+
}
|
|
80
|
+
// Recursively clean if the value is a non-null object
|
|
81
|
+
else if (value && typeof value === 'object') {
|
|
82
|
+
acc[key] = this.cleanObject(value);
|
|
83
|
+
}
|
|
84
|
+
// Otherwise, keep the value as-is
|
|
85
|
+
else {
|
|
86
|
+
acc[key] = value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return acc;
|
|
90
|
+
},
|
|
91
|
+
{} as Record<string, any>,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return cleanedObj as Partial<T>;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { GeinsCore } from '../geinsCore';
|
|
2
|
+
import { MerchantApiClient } from '../api-client/merchantApiClient';
|
|
3
|
+
import type { GeinsEventMessage, GeinsSettings } from '@geins/types';
|
|
4
|
+
import { GeinsEventType } from '@geins/types';
|
|
5
|
+
export abstract class BasePackage {
|
|
6
|
+
protected _geinsSettings: GeinsSettings;
|
|
7
|
+
protected _apiClient: () => MerchantApiClient;
|
|
8
|
+
|
|
9
|
+
constructor(protected core: GeinsCore) {
|
|
10
|
+
if (!core) {
|
|
11
|
+
throw new Error('Core is required');
|
|
12
|
+
}
|
|
13
|
+
if (!core.client) {
|
|
14
|
+
throw new Error('Merchant API Client is not set');
|
|
15
|
+
}
|
|
16
|
+
if (!core.geinsSettings) {
|
|
17
|
+
throw new Error('Settings are required');
|
|
18
|
+
}
|
|
19
|
+
const { client, geinsSettings } = core;
|
|
20
|
+
this._geinsSettings = geinsSettings;
|
|
21
|
+
this._apiClient = () => client ?? undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected pushEvent(
|
|
25
|
+
eventMessage: GeinsEventMessage,
|
|
26
|
+
eventName?: GeinsEventType,
|
|
27
|
+
) {
|
|
28
|
+
const eventNameStr = eventName ? GeinsEventType[eventName] : undefined;
|
|
29
|
+
if (eventNameStr && eventNameStr.includes('_')) {
|
|
30
|
+
const parentEvent = eventNameStr.split('_')[0];
|
|
31
|
+
this.core.events.push(eventMessage, parentEvent);
|
|
32
|
+
}
|
|
33
|
+
this.core.events.push(eventMessage, eventNameStr);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public getCore() {
|
|
37
|
+
return this.core;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export abstract class BaseStore {
|
|
2
|
+
store: any;
|
|
3
|
+
constructor() {
|
|
4
|
+
this.store = this.initStore();
|
|
5
|
+
}
|
|
6
|
+
abstract initStore(): any;
|
|
7
|
+
|
|
8
|
+
abstract getKey(key: string): Promise<any>;
|
|
9
|
+
|
|
10
|
+
abstract setKey(key: string, value: string): Promise<any>;
|
|
11
|
+
|
|
12
|
+
protected abstract deleteKey(key: string): Promise<any>;
|
|
13
|
+
|
|
14
|
+
protected abstract getKeys(): Promise<string[]>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface IStore {
|
|
18
|
+
setKey(key: string, value: string): Promise<void>;
|
|
19
|
+
getKey(key: string): Promise<string | null>;
|
|
20
|
+
getKeys(): Promise<string[]>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const AUTH_COOKIES = {
|
|
2
|
+
USER: 'geins-user',
|
|
3
|
+
USER_AUTH: 'geins-auth',
|
|
4
|
+
USER_TYPE: 'geins-user-type',
|
|
5
|
+
USER_MAX_AGE: 'geins-user-maxage',
|
|
6
|
+
REFRESH_TOKEN: 'geins-auth-refresh-token',
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const AUTH_COOKIES_MAX_AGE = {
|
|
10
|
+
DEFAULT: 1800,
|
|
11
|
+
REMEMBER_USER: 604800,
|
|
12
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const API_URL = 'https://merchantapi.geins.io/graphql';
|
|
2
|
+
const SIGN_URL = 'https://merchantapi.geins.io/auth/sign/{API-KEY}?identity=';
|
|
3
|
+
const AUTH_URL = 'https://auth-service.geins.io/api/{ACCOUNT}_{ENV}';
|
|
4
|
+
const IMAGE_URL = 'https://{ACCOUNT}.commerce.services';
|
|
5
|
+
const MANAGEMENT_API_URL = 'https://mgmtapi.geins.io';
|
|
6
|
+
const API_ENDPOINT_URL_HISTORY =
|
|
7
|
+
'https://merchantapi.geins.io/redirect/urlhistory';
|
|
8
|
+
const API_ENDPOINT_SLUG_HISTORY =
|
|
9
|
+
'https://merchantapi.geins.io/redirect/aliashistory';
|
|
10
|
+
|
|
11
|
+
const ENDPOINTS = {
|
|
12
|
+
main: API_URL,
|
|
13
|
+
auth: AUTH_URL,
|
|
14
|
+
auth_sign: SIGN_URL,
|
|
15
|
+
image: IMAGE_URL,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
ENDPOINTS,
|
|
20
|
+
MANAGEMENT_API_URL,
|
|
21
|
+
API_URL,
|
|
22
|
+
SIGN_URL,
|
|
23
|
+
AUTH_URL,
|
|
24
|
+
IMAGE_URL,
|
|
25
|
+
API_ENDPOINT_URL_HISTORY,
|
|
26
|
+
API_ENDPOINT_SLUG_HISTORY,
|
|
27
|
+
};
|
package/src/geinsCore.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type { GeinsSettings, GeinsChannelTypeType, GeinsEndpoints, Environment } from '@geins/types';
|
|
2
|
+
import { GeinsChannelInterface } from '@geins/types';
|
|
3
|
+
import { MerchantApiClient } from './api-client';
|
|
4
|
+
import { ChannelsService } from './services/channelsService';
|
|
5
|
+
import { CookieService } from './services/cookieService';
|
|
6
|
+
import { EventService } from './services/eventService';
|
|
7
|
+
import { Channel } from './logic';
|
|
8
|
+
import { isServerContext, buildEndpoints } from './utils';
|
|
9
|
+
import { GraphQLService } from './services';
|
|
10
|
+
|
|
11
|
+
export class GeinsCore {
|
|
12
|
+
// api client
|
|
13
|
+
private _endpointsUrls: any;
|
|
14
|
+
private _apiClient!: MerchantApiClient;
|
|
15
|
+
private _geinsSettings: GeinsSettings;
|
|
16
|
+
private _userToken?: string | undefined;
|
|
17
|
+
|
|
18
|
+
// exposed graphql service
|
|
19
|
+
private _graphQLService!: GraphQLService;
|
|
20
|
+
|
|
21
|
+
// cookie service
|
|
22
|
+
private _cookieService!: CookieService;
|
|
23
|
+
|
|
24
|
+
// events
|
|
25
|
+
private _eventService: EventService;
|
|
26
|
+
|
|
27
|
+
// channel
|
|
28
|
+
private _currentChannel!: Channel;
|
|
29
|
+
private _accountChannels!: ChannelsService;
|
|
30
|
+
|
|
31
|
+
constructor(geinsSettings: GeinsSettings) {
|
|
32
|
+
if (!geinsSettings.channel) {
|
|
33
|
+
throw new Error('Channel is required');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!geinsSettings.apiKey) {
|
|
37
|
+
throw new Error('API Key is required');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!geinsSettings.accountName) {
|
|
41
|
+
throw new Error('Account name is required');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Set default environment if not provided
|
|
45
|
+
const defaultSettings: Partial<GeinsSettings> = {
|
|
46
|
+
environment: 'prod',
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Merge provided settings with defaults
|
|
50
|
+
this._geinsSettings = { ...defaultSettings, ...geinsSettings };
|
|
51
|
+
|
|
52
|
+
// Initialize API Client
|
|
53
|
+
if (this._geinsSettings.apiKey && this._geinsSettings.accountName) {
|
|
54
|
+
this._endpointsUrls = buildEndpoints(
|
|
55
|
+
this._geinsSettings.apiKey,
|
|
56
|
+
this._geinsSettings.accountName,
|
|
57
|
+
this._geinsSettings.environment,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Initialize BroadcastChannel
|
|
62
|
+
if (!isServerContext()) {
|
|
63
|
+
this._cookieService = new CookieService();
|
|
64
|
+
}
|
|
65
|
+
this._currentChannel = new Channel(this._geinsSettings);
|
|
66
|
+
|
|
67
|
+
this._eventService = new EventService();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Initialize API Client
|
|
71
|
+
private initApiClient() {
|
|
72
|
+
if (this._geinsSettings.apiKey && this._geinsSettings.accountName) {
|
|
73
|
+
const options = {
|
|
74
|
+
apiUrl: this._endpointsUrls.main,
|
|
75
|
+
apiKey: this._geinsSettings.apiKey,
|
|
76
|
+
userToken: this._userToken,
|
|
77
|
+
};
|
|
78
|
+
this._apiClient = new MerchantApiClient(options);
|
|
79
|
+
} else {
|
|
80
|
+
throw new Error('Failed to initialize API Client');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private initGraphQLService(): void {
|
|
85
|
+
this._graphQLService = new GraphQLService(() => this.client, this._geinsSettings);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Channels
|
|
90
|
+
* Methods:
|
|
91
|
+
* - current() gets the current channel for application set from the settings;
|
|
92
|
+
* - all() get all channels for the account;
|
|
93
|
+
*/
|
|
94
|
+
get channel(): GeinsChannelInterface {
|
|
95
|
+
return {
|
|
96
|
+
current: this.channelGet.bind(this),
|
|
97
|
+
all: this.channelsGet.bind(this),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private async channelGet(): Promise<GeinsChannelTypeType | undefined> {
|
|
102
|
+
if (!this._currentChannel) {
|
|
103
|
+
this._currentChannel = new Channel(this._geinsSettings);
|
|
104
|
+
}
|
|
105
|
+
if (!this._currentChannel) {
|
|
106
|
+
throw new Error('Failed to initialize channel');
|
|
107
|
+
}
|
|
108
|
+
return this._currentChannel?.get() ?? undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async channelsGet(): Promise<GeinsChannelTypeType[] | undefined> {
|
|
112
|
+
if (!this._accountChannels) {
|
|
113
|
+
this._accountChannels = new ChannelsService(() => this.client, this._geinsSettings);
|
|
114
|
+
}
|
|
115
|
+
return this._accountChannels.get() ?? undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Set the user token.
|
|
120
|
+
* @param userToken
|
|
121
|
+
*/
|
|
122
|
+
public setUserToken(userToken?: string): void {
|
|
123
|
+
this._userToken = userToken;
|
|
124
|
+
if (this._apiClient) {
|
|
125
|
+
this._apiClient.updateToken(userToken);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Get the user token. Returns a token if user is authenticated.
|
|
131
|
+
* @returns string | undefined
|
|
132
|
+
*/
|
|
133
|
+
public getUserToken(): string | undefined {
|
|
134
|
+
return this._userToken;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Endpoints for the current environment.
|
|
139
|
+
* - main: The main api endpoint used to query Geins.
|
|
140
|
+
* - auth: The auth endpoint.
|
|
141
|
+
* - authSign: The auth sign endpoint.
|
|
142
|
+
* - image: The base image url
|
|
143
|
+
*/
|
|
144
|
+
get endpoints(): GeinsEndpoints {
|
|
145
|
+
return this._endpointsUrls;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Returns the API Client instance.
|
|
150
|
+
*/
|
|
151
|
+
get client(): MerchantApiClient {
|
|
152
|
+
if (!this._endpointsUrls) {
|
|
153
|
+
throw new Error('Endpoints are not set');
|
|
154
|
+
}
|
|
155
|
+
if (!this._apiClient) {
|
|
156
|
+
this.initApiClient();
|
|
157
|
+
}
|
|
158
|
+
return this._apiClient;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns the GraphQL Client instance.
|
|
162
|
+
* @returns GraphQLClient
|
|
163
|
+
* Use to query Geins using GraphQL.
|
|
164
|
+
*/
|
|
165
|
+
get graphql(): GraphQLService {
|
|
166
|
+
if (!this._graphQLService) {
|
|
167
|
+
this.initGraphQLService();
|
|
168
|
+
}
|
|
169
|
+
return this._graphQLService;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Returns the GeinsSettings that was used to to instance the class.
|
|
174
|
+
*/
|
|
175
|
+
get geinsSettings(): GeinsSettings {
|
|
176
|
+
return this._geinsSettings;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Returns the EventService instance.
|
|
181
|
+
* @returns EventService
|
|
182
|
+
* @example
|
|
183
|
+
* const eventService = core.events;
|
|
184
|
+
* eventService.listnerAdd((data) => {
|
|
185
|
+
* console.log(data);
|
|
186
|
+
* });
|
|
187
|
+
* eventService.push({
|
|
188
|
+
* subject: 'USER_LOGIN',
|
|
189
|
+
* payload: {
|
|
190
|
+
* user: 'luke.skywalker@tatooine.com',
|
|
191
|
+
* },
|
|
192
|
+
* broadcast: true,
|
|
193
|
+
* });
|
|
194
|
+
*/
|
|
195
|
+
get events(): EventService {
|
|
196
|
+
return this._eventService;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Returns the CookieService instance.
|
|
201
|
+
* @returns CookieService
|
|
202
|
+
* @example
|
|
203
|
+
* const cookieService = core.cookies;
|
|
204
|
+
* cookieService.set('cookieName', 'cookieValue');
|
|
205
|
+
* const cookieValue = cookieService.get('cookieName');
|
|
206
|
+
*
|
|
207
|
+
*/
|
|
208
|
+
get cookies(): CookieService {
|
|
209
|
+
if (!this._cookieService) {
|
|
210
|
+
this._cookieService = new CookieService();
|
|
211
|
+
}
|
|
212
|
+
return this._cookieService;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#import './fragments/channelFields.gql'
|
|
2
|
+
#import './fragments/languageFields.gql'
|
|
3
|
+
#import './fragments/marketFields.gql'
|
|
4
|
+
#import './fragments/currencyFields.gql'
|
|
5
|
+
|
|
6
|
+
query getChannel($channelId: String = null) {
|
|
7
|
+
channel(channelId: $channelId) {
|
|
8
|
+
...ChannelFields
|
|
9
|
+
markets {
|
|
10
|
+
...MarketFields
|
|
11
|
+
}
|
|
12
|
+
languages {
|
|
13
|
+
...LanguageFields
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#import './fragments/channelFields.gql'
|
|
2
|
+
#import './fragments/languageFields.gql'
|
|
3
|
+
#import './fragments/marketFields.gql'
|
|
4
|
+
#import './fragments/currencyFields.gql'
|
|
5
|
+
|
|
6
|
+
query getChannels {
|
|
7
|
+
channels {
|
|
8
|
+
...ChannelFields
|
|
9
|
+
markets {
|
|
10
|
+
...MarketFields
|
|
11
|
+
}
|
|
12
|
+
languages {
|
|
13
|
+
...LanguageFields
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#import './currencyFields.gql'
|
|
2
|
+
#import './countryFields.gql'
|
|
3
|
+
#import './languageFields.gql'
|
|
4
|
+
fragment MarketFields on MarketType {
|
|
5
|
+
id
|
|
6
|
+
alias
|
|
7
|
+
allowedLanguages {
|
|
8
|
+
...LanguageFields
|
|
9
|
+
}
|
|
10
|
+
country {
|
|
11
|
+
...CountryFields
|
|
12
|
+
}
|
|
13
|
+
currency {
|
|
14
|
+
...CurrencyFields
|
|
15
|
+
}
|
|
16
|
+
onlyDisplayInCheckout
|
|
17
|
+
virtual
|
|
18
|
+
groupKey
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './queries';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from '@geins/types';
|
|
2
|
+
export * from './api-client';
|
|
3
|
+
export * from './base';
|
|
4
|
+
export * from './constants';
|
|
5
|
+
export * from './geinsCore';
|
|
6
|
+
export * from './services';
|
|
7
|
+
export * from './stores';
|
|
8
|
+
export * from './utils';
|
|
9
|
+
export * from './logic';
|
|
10
|
+
export { gql } from '@apollo/client/core';
|