@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.
Files changed (88) hide show
  1. package/.turbo/turbo-build.log +7 -0
  2. package/CHANGELOG.md +13 -0
  3. package/README.md +1 -0
  4. package/__tests__/Channel.test.ts +34 -0
  5. package/__tests__/EventService.test.ts +32 -0
  6. package/__tests__/GeinsCore.test.ts +69 -0
  7. package/__tests__/Graphql.test.ts +43 -0
  8. package/dist/api-client/endpointClient.d.ts +8 -0
  9. package/dist/api-client/index.d.ts +4 -0
  10. package/dist/api-client/managementApiClient.d.ts +10 -0
  11. package/dist/api-client/merchantApiClient.d.ts +46 -0
  12. package/dist/base/baseApiService.d.ts +14 -0
  13. package/dist/base/basePackage.d.ts +12 -0
  14. package/dist/base/baseStore.d.ts +14 -0
  15. package/dist/base/index.d.ts +3 -0
  16. package/dist/constants/cookies.d.ts +11 -0
  17. package/dist/constants/endpoints.d.ts +14 -0
  18. package/dist/constants/headerNames.d.ts +3 -0
  19. package/dist/constants/index.d.ts +3 -0
  20. package/dist/geinsCore.d.ts +88 -0
  21. package/dist/graphql/index.d.ts +1 -0
  22. package/dist/graphql/queries.d.ts +5 -0
  23. package/dist/index.cjs +21783 -0
  24. package/dist/index.d.ts +10 -0
  25. package/dist/index.esm.js +21749 -0
  26. package/dist/logic/channel.d.ts +17 -0
  27. package/dist/logic/index.d.ts +1 -0
  28. package/dist/parsers/channelParser.d.ts +8 -0
  29. package/dist/services/channelService.d.ts +8 -0
  30. package/dist/services/channelsService.d.ts +8 -0
  31. package/dist/services/cookieService.d.ts +88 -0
  32. package/dist/services/eventService.d.ts +51 -0
  33. package/dist/services/graphQLService.d.ts +23 -0
  34. package/dist/services/index.d.ts +7 -0
  35. package/dist/services/logService.d.ts +19 -0
  36. package/dist/services/routingService.d.ts +74 -0
  37. package/dist/stores/channelStore.d.ts +11 -0
  38. package/dist/stores/index.d.ts +2 -0
  39. package/dist/stores/routingStore.d.ts +32 -0
  40. package/dist/utils/helpers.d.ts +21 -0
  41. package/dist/utils/index.d.ts +2 -0
  42. package/dist/utils/simpleCache.d.ts +9 -0
  43. package/eslint.config.js +3 -0
  44. package/jest.config.js +8 -0
  45. package/package.json +32 -0
  46. package/rollup.config.js +26 -0
  47. package/src/api-client/endpointClient.ts +52 -0
  48. package/src/api-client/index.ts +4 -0
  49. package/src/api-client/managementApiClient.ts +41 -0
  50. package/src/api-client/merchantApiClient.ts +153 -0
  51. package/src/base/baseApiService.ts +96 -0
  52. package/src/base/basePackage.ts +39 -0
  53. package/src/base/baseStore.ts +21 -0
  54. package/src/base/index.ts +3 -0
  55. package/src/constants/cookies.ts +12 -0
  56. package/src/constants/endpoints.ts +27 -0
  57. package/src/constants/headerNames.ts +3 -0
  58. package/src/constants/index.ts +3 -0
  59. package/src/geinsCore.ts +214 -0
  60. package/src/graphql/channel.gql +16 -0
  61. package/src/graphql/channels.gql +16 -0
  62. package/src/graphql/fragments/channelFields.gql +8 -0
  63. package/src/graphql/fragments/countryFields.gql +4 -0
  64. package/src/graphql/fragments/currencyFields.gql +4 -0
  65. package/src/graphql/fragments/languageFields.gql +5 -0
  66. package/src/graphql/fragments/marketFields.gql +19 -0
  67. package/src/graphql/graphql.d.ts +4 -0
  68. package/src/graphql/index.ts +1 -0
  69. package/src/graphql/queries.ts +9 -0
  70. package/src/index.ts +10 -0
  71. package/src/logic/channel.ts +102 -0
  72. package/src/logic/index.ts +1 -0
  73. package/src/parsers/channelParser.ts +91 -0
  74. package/src/services/channelService.ts +33 -0
  75. package/src/services/channelsService.ts +35 -0
  76. package/src/services/cookieService.ts +168 -0
  77. package/src/services/eventService.ts +89 -0
  78. package/src/services/graphQLService.ts +55 -0
  79. package/src/services/index.ts +7 -0
  80. package/src/services/logService.ts +434 -0
  81. package/src/services/routingService.ts +196 -0
  82. package/src/stores/channelStore.ts +34 -0
  83. package/src/stores/index.ts +2 -0
  84. package/src/stores/routingStore.ts +56 -0
  85. package/src/utils/helpers.ts +39 -0
  86. package/src/utils/index.ts +2 -0
  87. package/src/utils/simpleCache.ts +27 -0
  88. package/tsconfig.json +19 -0
@@ -0,0 +1,7 @@
1
+
2
+ yarn run v1.22.22
3
+ $ rollup -c
4
+ 
5
+ src/index.ts → dist/index.cjs, dist/index.esm.js...
6
+ created dist/index.cjs, dist/index.esm.js in 2.4s
7
+ ✨ Done in 2.89s.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @geins/core
2
+
3
+ ## 0.3.0-canary
4
+
5
+ ### Patch Changes
6
+
7
+ - first
8
+
9
+ ## 0.1.1-canary
10
+
11
+ ### Patch Changes
12
+
13
+ - first
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # CORE
@@ -0,0 +1,34 @@
1
+ // packages/sdk/core/__tests__/GeinsCore.test.ts
2
+
3
+ import { Channel } from '../src/logic/channel';
4
+ import { validSettings } from '../../../../test/globalSettings';
5
+
6
+ describe('Channel', () => {
7
+ let channel: Channel;
8
+ beforeEach(() => {
9
+ channel = Channel.getInstance(validSettings);
10
+ });
11
+
12
+ it('should initialize channel correctly', () => {
13
+ expect(channel).toBeDefined();
14
+ });
15
+
16
+ it('should get current channel correctly', async () => {
17
+ const currentChannel = await channel.get();
18
+ expect(currentChannel).toBeDefined();
19
+ expect(currentChannel).toHaveProperty('id');
20
+ expect(currentChannel).toHaveProperty('name');
21
+ expect(currentChannel).toHaveProperty('type');
22
+ expect(currentChannel).toHaveProperty('url');
23
+ expect(currentChannel).toHaveProperty('defaultMarketId');
24
+ expect(currentChannel).toHaveProperty('defaultLanguageId');
25
+ if (currentChannel) {
26
+ expect(currentChannel.id).toEqual(`${validSettings.channel}|${validSettings.tld}`);
27
+ }
28
+ });
29
+
30
+ afterEach(() => {
31
+ // Clean up mocks after each test
32
+ jest.clearAllMocks();
33
+ });
34
+ });
@@ -0,0 +1,32 @@
1
+ import { EventService } from '../src/services/eventService';
2
+ import { BroadcastChannel } from 'broadcast-channel';
3
+ import { EventEmitter } from 'events';
4
+ import { isServerContext } from '../src/utils';
5
+ import type { GeinsEventMessage } from '@geins/types';
6
+
7
+ // Mock dependencies
8
+ jest.mock('broadcast-channel');
9
+ jest.mock('events');
10
+ jest.mock('../src/utils', () => ({
11
+ isServerContext: jest.fn(),
12
+ }));
13
+
14
+ describe('EventService', () => {
15
+ let eventService: EventService;
16
+
17
+ beforeEach(() => {
18
+ // Set isServerContext to return false to simulate a browser environment
19
+ (isServerContext as jest.Mock).mockReturnValue(false);
20
+
21
+ // Create a new instance of EventService before each test
22
+ eventService = new EventService();
23
+ });
24
+
25
+ it('should be defined', () => {
26
+ expect(eventService).toBeDefined();
27
+ });
28
+
29
+ afterEach(() => {
30
+ jest.clearAllMocks(); // Clear all mocks after each test
31
+ });
32
+ });
@@ -0,0 +1,69 @@
1
+ // packages/sdk/core/__tests__/GeinsCore.test.ts
2
+
3
+ import { GeinsCore } from '../src/geinsCore';
4
+ import { GeinsSettings } from '@geins/types';
5
+ import { validSettings } from '../../../../test/globalSettings';
6
+
7
+ describe('GeinsCore', () => {
8
+ describe('Real API Calls', () => {
9
+ let geinsCore: GeinsCore;
10
+ beforeEach(() => {
11
+ geinsCore = new GeinsCore(validSettings);
12
+ // Reset mocks before each test
13
+ jest.clearAllMocks();
14
+ });
15
+ it('should list channels', async () => {
16
+ const channels = await geinsCore.channel.all();
17
+ expect(channels).toBeDefined();
18
+ expect(channels!.length).toBeGreaterThan(0);
19
+ });
20
+ });
21
+
22
+ it('should initialize with valid settings', () => {
23
+ expect(() => new GeinsCore(validSettings)).not.toThrow();
24
+ const geinsCore = new GeinsCore(validSettings);
25
+ expect(geinsCore).toBeInstanceOf(GeinsCore);
26
+ expect(geinsCore.geinsSettings).toEqual(validSettings);
27
+ });
28
+
29
+ it('should throw an error when channel is missing', () => {
30
+ const invalidSettings: Partial<GeinsSettings> = {
31
+ ...validSettings,
32
+ channel: undefined as any,
33
+ };
34
+ expect(() => new GeinsCore(invalidSettings as GeinsSettings)).toThrow('Channel is required');
35
+ });
36
+
37
+ it('should throw an error when API key is missing', () => {
38
+ const invalidSettings: Partial<GeinsSettings> = {
39
+ ...validSettings,
40
+ apiKey: undefined as any,
41
+ };
42
+ expect(() => new GeinsCore(invalidSettings as GeinsSettings)).toThrow('API Key is required');
43
+ });
44
+
45
+ it('should throw an error when Account name is missing', () => {
46
+ const invalidSettings: Partial<GeinsSettings> = {
47
+ ...validSettings,
48
+ accountName: undefined as any,
49
+ };
50
+ expect(() => new GeinsCore(invalidSettings as GeinsSettings)).toThrow('Account name is required');
51
+ });
52
+
53
+ it('should use default environment when not specified', () => {
54
+ const settingsWithoutEnvironment: Omit<GeinsSettings, 'environment'> = {
55
+ ...validSettings,
56
+ };
57
+ delete (settingsWithoutEnvironment as any).environment;
58
+ const geinsCore = new GeinsCore(settingsWithoutEnvironment);
59
+ expect(geinsCore.geinsSettings.environment).toBe('prod');
60
+ });
61
+
62
+ it('should correctly set up endpoints', () => {
63
+ const geinsCore = new GeinsCore(validSettings);
64
+ expect(geinsCore.endpoints).toHaveProperty('main');
65
+ expect(geinsCore.endpoints).toHaveProperty('auth');
66
+ expect(geinsCore.endpoints).toHaveProperty('authSign');
67
+ expect(geinsCore.endpoints).toHaveProperty('image');
68
+ });
69
+ });
@@ -0,0 +1,43 @@
1
+ import { GeinsCore, GraphQLService, gql } from '../src';
2
+ import { validSettings } from '../../../../test/globalSettings';
3
+
4
+ describe('GeinsCMS', () => {
5
+ let graphqlClient: GraphQLService;
6
+
7
+ beforeEach(() => {
8
+ const geinsCore = new GeinsCore(validSettings);
9
+ graphqlClient = geinsCore.graphql;
10
+
11
+ // Reset mocks before each test
12
+ jest.clearAllMocks();
13
+ });
14
+ afterEach(() => {
15
+ jest.clearAllMocks();
16
+ });
17
+
18
+ it('should get result from custom query', async () => {
19
+ const MY_QUERY = gql`
20
+ query getChannels {
21
+ channels {
22
+ name
23
+ url
24
+ }
25
+ }
26
+ `;
27
+
28
+ interface Channels {
29
+ channels: {
30
+ name: string;
31
+ url: string;
32
+ }[];
33
+ }
34
+
35
+ const result = await graphqlClient.query<Channels>({
36
+ query: MY_QUERY,
37
+ });
38
+
39
+ expect(result).toBeDefined();
40
+ expect(result!.channels).toBeDefined();
41
+ expect(result!.channels.length).toBeGreaterThan(0);
42
+ });
43
+ });
@@ -0,0 +1,8 @@
1
+ export declare class EndpointApiClient {
2
+ private apiKey;
3
+ constructor(apiKey: string);
4
+ private getEndpointUrl;
5
+ private request;
6
+ getUrlHistory(lastFetchTime?: string): Promise<any>;
7
+ getSlugHistory(lastFetchTime?: string): Promise<any>;
8
+ }
@@ -0,0 +1,4 @@
1
+ export * from './merchantApiClient';
2
+ export * from './managementApiClient';
3
+ export * from './endpointClient';
4
+ export * from '../constants/endpoints';
@@ -0,0 +1,10 @@
1
+ import type { ManagementApiCredentials } from '@geins/types';
2
+ export declare class ManagementApiClient {
3
+ private baseUrl;
4
+ private apiKey;
5
+ private authToken;
6
+ constructor(credentials: ManagementApiCredentials);
7
+ createAuthToken(credentials: ManagementApiCredentials): string;
8
+ getEndpointUrl(path: string): string;
9
+ request(method: string, path: string, data?: any): Promise<any>;
10
+ }
@@ -0,0 +1,46 @@
1
+ import { ApolloClient, NormalizedCacheObject, FetchPolicy, ApolloQueryResult, FetchResult, OperationVariables } from '@apollo/client/core';
2
+ export declare enum FetchPolicyOptions {
3
+ CACHE_FIRST = "cache-first",
4
+ NETWORK_ONLY = "network-only",
5
+ CACHE_ONLY = "cache-only",
6
+ NO_CACHE = "no-cache",
7
+ STANDBY = "standby"
8
+ }
9
+ export declare enum OperationType {
10
+ QUERY = "query",
11
+ MUTATION = "mutation"
12
+ }
13
+ export interface RequestOptions {
14
+ fetchPolicy?: FetchPolicy;
15
+ pollInterval?: number;
16
+ context?: any;
17
+ [key: string]: any;
18
+ }
19
+ export interface MerchantApiClientOptions {
20
+ apiUrl: string;
21
+ apiKey: string;
22
+ fetchPolicy?: FetchPolicy;
23
+ userToken?: string;
24
+ }
25
+ export interface GraphQLQueryOptions {
26
+ query?: any;
27
+ variables?: any;
28
+ requestOptions?: RequestOptions;
29
+ }
30
+ export declare class MerchantApiClient {
31
+ private _cookieService;
32
+ private _apolloClient;
33
+ private _userToken?;
34
+ fetchPolicy: FetchPolicy;
35
+ pollInterval: number;
36
+ constructor(options: MerchantApiClientOptions);
37
+ updateToken(newToken?: string): void;
38
+ get userToken(): string | undefined;
39
+ createClient(apiUrl: string, apiKey: string): ApolloClient<NormalizedCacheObject>;
40
+ getClient(): ApolloClient<NormalizedCacheObject> | undefined;
41
+ clearCacheAndRefetchQueries(): void;
42
+ private getFetchPolicy;
43
+ private getOperationObject;
44
+ runQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(options: GraphQLQueryOptions): Promise<ApolloQueryResult<TData>>;
45
+ runMutation<TData = any, TVariables extends OperationVariables = OperationVariables>(options: GraphQLQueryOptions): Promise<FetchResult<TData>>;
46
+ }
@@ -0,0 +1,14 @@
1
+ import type { GeinsSettings } from '@geins/types';
2
+ import { GraphQLQueryOptions } from '../api-client/merchantApiClient';
3
+ export declare abstract class BaseApiService {
4
+ protected _apiClient: any;
5
+ protected _geinsSettings: GeinsSettings;
6
+ protected _channelId: string;
7
+ constructor(apiClient: any, geinsSettings: GeinsSettings);
8
+ protected createVariables(vars: any): any;
9
+ protected runQuery(options: GraphQLQueryOptions): Promise<any>;
10
+ protected runQueryParsed<T>(options: GraphQLQueryOptions): Promise<T>;
11
+ protected runMutation(options: GraphQLQueryOptions): Promise<any>;
12
+ protected parseResult(result: any): any;
13
+ protected cleanObject<T extends Record<string, any>>(obj: T): Partial<T>;
14
+ }
@@ -0,0 +1,12 @@
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 declare abstract class BasePackage {
6
+ protected core: GeinsCore;
7
+ protected _geinsSettings: GeinsSettings;
8
+ protected _apiClient: () => MerchantApiClient;
9
+ constructor(core: GeinsCore);
10
+ protected pushEvent(eventMessage: GeinsEventMessage, eventName?: GeinsEventType): void;
11
+ getCore(): GeinsCore;
12
+ }
@@ -0,0 +1,14 @@
1
+ export declare abstract class BaseStore {
2
+ store: any;
3
+ constructor();
4
+ abstract initStore(): any;
5
+ abstract getKey(key: string): Promise<any>;
6
+ abstract setKey(key: string, value: string): Promise<any>;
7
+ protected abstract deleteKey(key: string): Promise<any>;
8
+ protected abstract getKeys(): Promise<string[]>;
9
+ }
10
+ export interface IStore {
11
+ setKey(key: string, value: string): Promise<void>;
12
+ getKey(key: string): Promise<string | null>;
13
+ getKeys(): Promise<string[]>;
14
+ }
@@ -0,0 +1,3 @@
1
+ export * from './baseApiService';
2
+ export * from './basePackage';
3
+ export * from './baseStore';
@@ -0,0 +1,11 @@
1
+ export declare const AUTH_COOKIES: {
2
+ USER: string;
3
+ USER_AUTH: string;
4
+ USER_TYPE: string;
5
+ USER_MAX_AGE: string;
6
+ REFRESH_TOKEN: string;
7
+ };
8
+ export declare const AUTH_COOKIES_MAX_AGE: {
9
+ DEFAULT: number;
10
+ REMEMBER_USER: number;
11
+ };
@@ -0,0 +1,14 @@
1
+ declare const API_URL = "https://merchantapi.geins.io/graphql";
2
+ declare const SIGN_URL = "https://merchantapi.geins.io/auth/sign/{API-KEY}?identity=";
3
+ declare const AUTH_URL = "https://auth-service.geins.io/api/{ACCOUNT}_{ENV}";
4
+ declare const IMAGE_URL = "https://{ACCOUNT}.commerce.services";
5
+ declare const MANAGEMENT_API_URL = "https://mgmtapi.geins.io";
6
+ declare const API_ENDPOINT_URL_HISTORY = "https://merchantapi.geins.io/redirect/urlhistory";
7
+ declare const API_ENDPOINT_SLUG_HISTORY = "https://merchantapi.geins.io/redirect/aliashistory";
8
+ declare const ENDPOINTS: {
9
+ main: string;
10
+ auth: string;
11
+ auth_sign: string;
12
+ image: string;
13
+ };
14
+ export { ENDPOINTS, MANAGEMENT_API_URL, API_URL, SIGN_URL, AUTH_URL, IMAGE_URL, API_ENDPOINT_URL_HISTORY, API_ENDPOINT_SLUG_HISTORY, };
@@ -0,0 +1,3 @@
1
+ export declare const AUTH_HEADERS: {
2
+ REFRESH_TOKEN: string;
3
+ };
@@ -0,0 +1,3 @@
1
+ export * from './endpoints';
2
+ export * from './cookies';
3
+ export * from './headerNames';
@@ -0,0 +1,88 @@
1
+ import type { GeinsSettings, GeinsEndpoints } from '@geins/types';
2
+ import { GeinsChannelInterface } from '@geins/types';
3
+ import { MerchantApiClient } from './api-client';
4
+ import { CookieService } from './services/cookieService';
5
+ import { EventService } from './services/eventService';
6
+ import { GraphQLService } from './services';
7
+ export declare class GeinsCore {
8
+ private _endpointsUrls;
9
+ private _apiClient;
10
+ private _geinsSettings;
11
+ private _userToken?;
12
+ private _graphQLService;
13
+ private _cookieService;
14
+ private _eventService;
15
+ private _currentChannel;
16
+ private _accountChannels;
17
+ constructor(geinsSettings: GeinsSettings);
18
+ private initApiClient;
19
+ private initGraphQLService;
20
+ /**
21
+ * Channels
22
+ * Methods:
23
+ * - current() gets the current channel for application set from the settings;
24
+ * - all() get all channels for the account;
25
+ */
26
+ get channel(): GeinsChannelInterface;
27
+ private channelGet;
28
+ private channelsGet;
29
+ /**
30
+ * Set the user token.
31
+ * @param userToken
32
+ */
33
+ setUserToken(userToken?: string): void;
34
+ /**
35
+ * Get the user token. Returns a token if user is authenticated.
36
+ * @returns string | undefined
37
+ */
38
+ getUserToken(): string | undefined;
39
+ /**
40
+ * Endpoints for the current environment.
41
+ * - main: The main api endpoint used to query Geins.
42
+ * - auth: The auth endpoint.
43
+ * - authSign: The auth sign endpoint.
44
+ * - image: The base image url
45
+ */
46
+ get endpoints(): GeinsEndpoints;
47
+ /**
48
+ * Returns the API Client instance.
49
+ */
50
+ get client(): MerchantApiClient;
51
+ /**
52
+ * Returns the GraphQL Client instance.
53
+ * @returns GraphQLClient
54
+ * Use to query Geins using GraphQL.
55
+ */
56
+ get graphql(): GraphQLService;
57
+ /**
58
+ * Returns the GeinsSettings that was used to to instance the class.
59
+ */
60
+ get geinsSettings(): GeinsSettings;
61
+ /**
62
+ * Returns the EventService instance.
63
+ * @returns EventService
64
+ * @example
65
+ * const eventService = core.events;
66
+ * eventService.listnerAdd((data) => {
67
+ * console.log(data);
68
+ * });
69
+ * eventService.push({
70
+ * subject: 'USER_LOGIN',
71
+ * payload: {
72
+ * user: 'luke.skywalker@tatooine.com',
73
+ * },
74
+ * broadcast: true,
75
+ * });
76
+ */
77
+ get events(): EventService;
78
+ /**
79
+ * Returns the CookieService instance.
80
+ * @returns CookieService
81
+ * @example
82
+ * const cookieService = core.cookies;
83
+ * cookieService.set('cookieName', 'cookieValue');
84
+ * const cookieValue = cookieService.get('cookieName');
85
+ *
86
+ */
87
+ get cookies(): CookieService;
88
+ }
@@ -0,0 +1 @@
1
+ export * from './queries';
@@ -0,0 +1,5 @@
1
+ declare const queries: {
2
+ channels: string;
3
+ channel: string;
4
+ };
5
+ export { queries };