@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,17 @@
|
|
|
1
|
+
import type { GeinsSettings, GeinsChannelTypeType } from '@geins/types';
|
|
2
|
+
export declare class Channel {
|
|
3
|
+
private geinsSettings;
|
|
4
|
+
private channelId;
|
|
5
|
+
private channelService;
|
|
6
|
+
private cache;
|
|
7
|
+
private store;
|
|
8
|
+
private _apiClient;
|
|
9
|
+
constructor(geinsSettings: GeinsSettings);
|
|
10
|
+
private initApiClient;
|
|
11
|
+
private initChannelService;
|
|
12
|
+
static getInstance(geinsSettings: GeinsSettings): Channel;
|
|
13
|
+
setKey(key: string, value: string): void;
|
|
14
|
+
getKey(key: string): Promise<string>;
|
|
15
|
+
private setChannel;
|
|
16
|
+
get(): Promise<GeinsChannelTypeType | undefined>;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './channel';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GeinsChannelTypeType, GeinsMarketTypeType, GeinsCountryTypeType, GeinsLanguageTypeType, GeinsCurrencyTypeType } from '@geins/types';
|
|
2
|
+
export declare function parseChannelsResult(result: any): GeinsChannelTypeType[] | undefined;
|
|
3
|
+
export declare function parseChannelResult(result: any): GeinsChannelTypeType | undefined;
|
|
4
|
+
export declare function parseChannel(channel: any): GeinsChannelTypeType | undefined;
|
|
5
|
+
export declare function parseMarket(market: any): GeinsMarketTypeType;
|
|
6
|
+
export declare function parseCountry(country: any): GeinsCountryTypeType;
|
|
7
|
+
export declare function parseLanguage(language: any): GeinsLanguageTypeType;
|
|
8
|
+
export declare function parseCurrency(currency: any): GeinsCurrencyTypeType;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GeinsChannelTypeType, GeinsSettings } from '@geins/types';
|
|
2
|
+
import { BaseApiService } from '../base/baseApiService';
|
|
3
|
+
export declare class ChannelService extends BaseApiService {
|
|
4
|
+
private cache;
|
|
5
|
+
constructor(client: any, geinsSettings: GeinsSettings);
|
|
6
|
+
get(channelId: string): Promise<GeinsChannelTypeType | undefined>;
|
|
7
|
+
protected parseResult(result: any): GeinsChannelTypeType | undefined;
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GeinsChannelTypeType, GeinsSettings } from '@geins/types';
|
|
2
|
+
import { BaseApiService } from '../base/baseApiService';
|
|
3
|
+
export declare class ChannelsService extends BaseApiService {
|
|
4
|
+
private cache;
|
|
5
|
+
constructor(client: any, geinsSettings: GeinsSettings);
|
|
6
|
+
get(): Promise<GeinsChannelTypeType[] | undefined>;
|
|
7
|
+
protected parseResult(result: any): GeinsChannelTypeType[] | undefined;
|
|
8
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for setting cookies.
|
|
3
|
+
*/
|
|
4
|
+
export interface CookieServiceConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Indicates if the cookie is HTTP only.
|
|
7
|
+
* If `true`, the cookie is inaccessible to client-side scripts.
|
|
8
|
+
*/
|
|
9
|
+
httpOnly?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Specifies the domain for which the cookie is valid.
|
|
12
|
+
* If not specified, defaults to the host portion of the current document URL.
|
|
13
|
+
*/
|
|
14
|
+
domain?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Specifies the URL path that must exist in the requested URL for the browser to send the Cookie header.
|
|
17
|
+
* Defaults to `'/'`.
|
|
18
|
+
*/
|
|
19
|
+
path?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Indicates if the cookie should only be transmitted over secure protocols like HTTPS.
|
|
22
|
+
* If `true`, the cookie will only be sent over secure connections.
|
|
23
|
+
*/
|
|
24
|
+
secure?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Sets the maximum age of the cookie in seconds.
|
|
27
|
+
* Can be a number or a string representation of a number.
|
|
28
|
+
*/
|
|
29
|
+
maxAge?: number | string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Represents a cookie with a name and payload, extending the cookie configuration options.
|
|
33
|
+
*/
|
|
34
|
+
export interface CookieType extends CookieServiceConfig {
|
|
35
|
+
/**
|
|
36
|
+
* The name of the cookie.
|
|
37
|
+
*/
|
|
38
|
+
name: string;
|
|
39
|
+
/**
|
|
40
|
+
* The value or data stored in the cookie.
|
|
41
|
+
*/
|
|
42
|
+
payload: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Service for managing cookies.
|
|
46
|
+
*/
|
|
47
|
+
export declare class CookieService {
|
|
48
|
+
private path;
|
|
49
|
+
private domain;
|
|
50
|
+
private secure;
|
|
51
|
+
private httpOnly;
|
|
52
|
+
private maxAge;
|
|
53
|
+
private cookie;
|
|
54
|
+
/**
|
|
55
|
+
* Creates an instance of CookieService.
|
|
56
|
+
* @param {CookieServiceConfig} [config] - Optional configuration for the cookie service.
|
|
57
|
+
*/
|
|
58
|
+
constructor(config?: CookieServiceConfig);
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves the current cookie configuration.
|
|
61
|
+
* @returns {CookieServiceConfig} - The current configuration settings.
|
|
62
|
+
*/
|
|
63
|
+
protected getConfig(): CookieServiceConfig;
|
|
64
|
+
private parseMaxAge;
|
|
65
|
+
private configToCookieOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Retrieves all cookies.
|
|
68
|
+
* @returns {Record<string, string>} - An object containing all cookies.
|
|
69
|
+
*/
|
|
70
|
+
getAll(): Record<string, string>;
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves the value of a specific cookie.
|
|
73
|
+
* @param {string} cookieName - The name of the cookie to retrieve.
|
|
74
|
+
* @returns {string} - The value of the cookie, or an empty string if not found.
|
|
75
|
+
*/
|
|
76
|
+
get(cookieName: string): string;
|
|
77
|
+
/**
|
|
78
|
+
* Removes a specific cookie.
|
|
79
|
+
* @param {string} cookieName - The name of the cookie to remove.
|
|
80
|
+
*/
|
|
81
|
+
remove(cookieName: string): void;
|
|
82
|
+
/**
|
|
83
|
+
* Sets a cookie with the specified options.
|
|
84
|
+
* @param {CookieType} cookie - The cookie to set, including name and payload.
|
|
85
|
+
* @param {CookieServiceConfig} [config] - Optional configuration for setting the cookie.
|
|
86
|
+
*/
|
|
87
|
+
set(cookie: CookieType, config?: CookieServiceConfig): void;
|
|
88
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { GeinsEventMessage } from '@geins/types';
|
|
2
|
+
/**
|
|
3
|
+
* Event service
|
|
4
|
+
* Responsible for handling events and broadcasting them to other windows and tabs.
|
|
5
|
+
*
|
|
6
|
+
* @class
|
|
7
|
+
* @name EventService
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare class EventService {
|
|
11
|
+
private eventName;
|
|
12
|
+
private emitter;
|
|
13
|
+
private broadcast;
|
|
14
|
+
private broadcastChannelId;
|
|
15
|
+
constructor();
|
|
16
|
+
private addBroadcastListener;
|
|
17
|
+
/**
|
|
18
|
+
* Adds a presistent event listener to the event emitter.
|
|
19
|
+
* @param handler A callback function that handles the event data.
|
|
20
|
+
* @param eventName (optional): The name of the event to listen for. If omitted, it defaults to 'geins-event'.
|
|
21
|
+
*/
|
|
22
|
+
listnerAdd(handler: any, eventName?: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Adds a one-time event listener for the specified event. This listener will be invoked only the first time the event is emitted and then automatically removed.
|
|
25
|
+
* @param handler A callback function that handles the event data.
|
|
26
|
+
* @param eventName (optional): The name of the event to listen for. If omitted, it defaults to 'geins-event'.
|
|
27
|
+
*/
|
|
28
|
+
listnerOnce(handler: any, eventName?: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* Removes the specified event listener from the event emitter. *
|
|
31
|
+
* @param eventName (optional): The name of the event to listen for. If omitted, it defaults to 'geins-event'.
|
|
32
|
+
*/
|
|
33
|
+
listnerRemove(eventName?: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the number of listeners for the specified event.
|
|
36
|
+
* @param eventName (optional): The name of the event to listen for. If omitted, it defaults to 'geins-event'.
|
|
37
|
+
*/
|
|
38
|
+
listnerCount(eventName?: string): number;
|
|
39
|
+
/**
|
|
40
|
+
* Lists the listeners for the specified event.
|
|
41
|
+
* @param eventName
|
|
42
|
+
* @returns An array of listeners for the specified event.
|
|
43
|
+
*/
|
|
44
|
+
listnersGet(eventName?: string): any[];
|
|
45
|
+
/**
|
|
46
|
+
* Pushes an event message to the event emitter.
|
|
47
|
+
* @param eventMessage - The event message to push.
|
|
48
|
+
* @param eventName - The name of the event to push the message to. If omitted, it defaults to 'geins-event'.
|
|
49
|
+
*/
|
|
50
|
+
push(eventMessage: GeinsEventMessage, eventName?: string): void;
|
|
51
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { GeinsSettings } from '@geins/types';
|
|
2
|
+
import { BaseApiService } from '../base/baseApiService';
|
|
3
|
+
import { GraphQLQueryOptions } from '../api-client/merchantApiClient';
|
|
4
|
+
export declare class GraphQLService extends BaseApiService {
|
|
5
|
+
constructor(client: any, geinsSettings: GeinsSettings);
|
|
6
|
+
/**
|
|
7
|
+
* Generic method to run any GraphQL query
|
|
8
|
+
* @param query - The GraphQL query DocumentNode or gql query object
|
|
9
|
+
* @param variables - Variables to pass to the query
|
|
10
|
+
* @param options - Optional options like fetchPolicy, pollInterval, etc.
|
|
11
|
+
* @returns The result data of the query or null
|
|
12
|
+
*/
|
|
13
|
+
query<T = any>(options: GraphQLQueryOptions): Promise<T | null>;
|
|
14
|
+
/**
|
|
15
|
+
* Generic method to run any GraphQL mutation
|
|
16
|
+
* @param mutation - The GraphQL mutation DocumentNode or gql mutation object
|
|
17
|
+
* @param variables - Variables to pass to the mutation
|
|
18
|
+
* @param options - Optional options like fetchPolicy, etc.
|
|
19
|
+
* @returns The result data of the mutation or null
|
|
20
|
+
*/
|
|
21
|
+
mutation<T = any>(options: GraphQLQueryOptions): Promise<T | null>;
|
|
22
|
+
protected parseResult(result: any): any;
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare class LogService {
|
|
2
|
+
private static configIsEnabled;
|
|
3
|
+
private static configLogLevel;
|
|
4
|
+
static configure({ isEnabled, logLevel, }?: {
|
|
5
|
+
isEnabled?: boolean;
|
|
6
|
+
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
7
|
+
}): void;
|
|
8
|
+
static isServer(): boolean;
|
|
9
|
+
static trace(...args: any[]): void;
|
|
10
|
+
static debug(...args: any[]): void;
|
|
11
|
+
static info(...args: any[]): void;
|
|
12
|
+
static warn(...args: any[]): void;
|
|
13
|
+
static error(...args: any[]): void;
|
|
14
|
+
private static log;
|
|
15
|
+
private static output;
|
|
16
|
+
private static getCallerInfoExtended;
|
|
17
|
+
private static getFileTypeFromExtension;
|
|
18
|
+
}
|
|
19
|
+
export declare const logWrite: typeof LogService.debug;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { IStore } from '../base/baseStore';
|
|
2
|
+
export declare enum RoutingServiceState {
|
|
3
|
+
INIT = 0,
|
|
4
|
+
FETCHING = 1,
|
|
5
|
+
READY = 2,
|
|
6
|
+
ERROR = 3
|
|
7
|
+
}
|
|
8
|
+
export interface RoutingRule extends RoutingItem {
|
|
9
|
+
httpStatusCode?: number;
|
|
10
|
+
isCanonical?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface RoutingItem {
|
|
13
|
+
fromUrl: string;
|
|
14
|
+
toUrl: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class RoutingService {
|
|
17
|
+
private static instance;
|
|
18
|
+
private store;
|
|
19
|
+
private apiKey;
|
|
20
|
+
private apiClient;
|
|
21
|
+
private state;
|
|
22
|
+
private constructor();
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the singleton instance of the RoutingService.
|
|
25
|
+
* @param apiKey - The API key used for authentication.
|
|
26
|
+
* @param store - The storage interface for caching routes.
|
|
27
|
+
* @returns The singleton instance of RoutingService.
|
|
28
|
+
*/
|
|
29
|
+
static getInstance(apiKey: string, store: IStore): RoutingService;
|
|
30
|
+
private setKey;
|
|
31
|
+
private getKey;
|
|
32
|
+
/**
|
|
33
|
+
* Retrieves all routing rules from the store.
|
|
34
|
+
* @returns A promise that resolves to an array of RoutingRule objects.
|
|
35
|
+
*/
|
|
36
|
+
getRoutingRules(): Promise<RoutingRule[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Retrieves all route keys from the store.
|
|
39
|
+
* @returns A promise that resolves to an array of route keys.
|
|
40
|
+
*/
|
|
41
|
+
getAllRoutes(): Promise<string[]>;
|
|
42
|
+
private shouldFetchNewData;
|
|
43
|
+
private fetchAndProcessUrlHistory;
|
|
44
|
+
/**
|
|
45
|
+
* Fetches the URL history from the API and updates the store.
|
|
46
|
+
* If the history was fetched less than an hour ago, it uses the cached data.
|
|
47
|
+
* @returns A promise that resolves to an array of route keys.
|
|
48
|
+
* @throws An error if the URL history could not be fetched.
|
|
49
|
+
*/
|
|
50
|
+
fillUrlHistory(): Promise<string[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Retrieves the route for the given path from the store.
|
|
53
|
+
* @param path - The path for which to get the route.
|
|
54
|
+
* @returns A promise that resolves to the route or null if not found.
|
|
55
|
+
*/
|
|
56
|
+
getRoute(path: string): Promise<string | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Refreshes the URL history if more than an hour has passed since the last fetch.
|
|
59
|
+
* @returns A promise that resolves to an array of route keys.
|
|
60
|
+
*/
|
|
61
|
+
refreshUrlHistoryIfNeeded(): Promise<string[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves the last time the URL history was fetched.
|
|
64
|
+
* @returns A promise that resolves to the last fetch time as a string or null if not set.
|
|
65
|
+
*/
|
|
66
|
+
getLastFetchTime(): Promise<string | null>;
|
|
67
|
+
/**
|
|
68
|
+
* Fills the routes by fetching the URL history and returns the updated routes.
|
|
69
|
+
* @returns A promise that resolves to an object containing the urlHistory array.
|
|
70
|
+
*/
|
|
71
|
+
fillRoutes(): Promise<{
|
|
72
|
+
urlHistory: string[];
|
|
73
|
+
}>;
|
|
74
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import NodeCache from 'node-cache';
|
|
2
|
+
import { BaseStore } from '../base';
|
|
3
|
+
export declare class ChannelStore extends BaseStore {
|
|
4
|
+
store: NodeCache;
|
|
5
|
+
constructor();
|
|
6
|
+
initStore(): NodeCache;
|
|
7
|
+
getKey(from: string): Promise<any>;
|
|
8
|
+
setKey(from: string, to: string): Promise<boolean>;
|
|
9
|
+
deleteKey(from: string): Promise<number>;
|
|
10
|
+
getKeys(): Promise<string[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { IStore } from '../base';
|
|
2
|
+
/**
|
|
3
|
+
* RoutingStore implements the IStore interface using NodeCache for in-memory caching.
|
|
4
|
+
*/
|
|
5
|
+
export declare class RoutingStore implements IStore {
|
|
6
|
+
private store;
|
|
7
|
+
constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Sets a key-value pair in the cache.
|
|
10
|
+
* @param key - The key under which the value is stored.
|
|
11
|
+
* @param value - The value to store.
|
|
12
|
+
* @returns A promise that resolves when the operation is complete.
|
|
13
|
+
*/
|
|
14
|
+
setKey(key: string, value: string): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves a value from the cache by its key.
|
|
17
|
+
* @param key - The key of the value to retrieve.
|
|
18
|
+
* @returns A promise that resolves to the value or null if not found.
|
|
19
|
+
*/
|
|
20
|
+
getKey(key: string): Promise<string | null>;
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves all keys from the cache.
|
|
23
|
+
* @returns A promise that resolves to an array of all keys.
|
|
24
|
+
*/
|
|
25
|
+
getKeys(): Promise<string[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Deletes a key-value pair from the cache.
|
|
28
|
+
* @param key - The key of the value to delete.
|
|
29
|
+
* @returns A promise that resolves when the operation is complete.
|
|
30
|
+
*/
|
|
31
|
+
deleteKey(key: string): Promise<void>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Environment, GeinsEndpoints } from '@geins/types';
|
|
2
|
+
/**
|
|
3
|
+
* Get context of the runtime.
|
|
4
|
+
* @returns If the context is server.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isServerContext(): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Builds the endpoints for the Geins API.
|
|
9
|
+
* @param apiKey - The API key.
|
|
10
|
+
* @param accountName - The account name.
|
|
11
|
+
* @param environment - The environment.
|
|
12
|
+
* @returns The endpoints.
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const apiKey
|
|
16
|
+
* const accountName
|
|
17
|
+
* const environment
|
|
18
|
+
* const endpoints = buildEndpoints(apiKey, accountName, environment);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function buildEndpoints(apiKey: string, accountName: string, environment?: Environment): GeinsEndpoints;
|
package/eslint.config.js
ADDED
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@geins/core",
|
|
3
|
+
"version": "0.3.0-canary",
|
|
4
|
+
"main": "dist/index.cjs",
|
|
5
|
+
"module": "dist/index.esm.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "rollup -c",
|
|
11
|
+
"prepublishOnly": "yarn build",
|
|
12
|
+
"lint": "eslint --fix .",
|
|
13
|
+
"link": "yarn link"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@apollo/client": "^3.11.8",
|
|
17
|
+
"cookie-universal": "^2.2.2",
|
|
18
|
+
"graphql": "^16.9.0",
|
|
19
|
+
"node-cache": "^5.1.2",
|
|
20
|
+
"broadcast-channel": "^7.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@geins/types": "0.3.0-canary",
|
|
24
|
+
"@geins/eslint-config": "*",
|
|
25
|
+
"@rollup/plugin-commonjs": "^28.0.0",
|
|
26
|
+
"@rollup/plugin-graphql": "^2.0.5",
|
|
27
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
28
|
+
"@rollup/plugin-typescript": "^12.1.0",
|
|
29
|
+
"typescript": "^5.6.2",
|
|
30
|
+
"eslint": "^9.11.1"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
import typescript from '@rollup/plugin-typescript';
|
|
4
|
+
import graphql from '@rollup/plugin-graphql';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
input: 'src/index.ts',
|
|
8
|
+
output: [
|
|
9
|
+
{
|
|
10
|
+
file: 'dist/index.cjs',
|
|
11
|
+
format: 'cjs',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
file: 'dist/index.esm.js',
|
|
15
|
+
format: 'esm',
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
plugins: [
|
|
19
|
+
resolve({
|
|
20
|
+
preferBuiltins: true,
|
|
21
|
+
}),
|
|
22
|
+
commonjs(),
|
|
23
|
+
typescript(),
|
|
24
|
+
graphql(),
|
|
25
|
+
],
|
|
26
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
API_ENDPOINT_URL_HISTORY,
|
|
3
|
+
API_ENDPOINT_SLUG_HISTORY,
|
|
4
|
+
} from '../constants';
|
|
5
|
+
export class EndpointApiClient {
|
|
6
|
+
private apiKey: string;
|
|
7
|
+
|
|
8
|
+
constructor(apiKey: string) {
|
|
9
|
+
this.apiKey = apiKey;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private getEndpointUrl(endpointUrl: string) {
|
|
13
|
+
return `${endpointUrl}/${this.apiKey}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private async request(endpointUrl: string) {
|
|
17
|
+
try {
|
|
18
|
+
const options = {
|
|
19
|
+
headers: {
|
|
20
|
+
Accept: 'application/json',
|
|
21
|
+
'Content-Type': 'application/json',
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
const response = await fetch(endpointUrl, options);
|
|
25
|
+
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Request failed with status ${response.status}: ${response.statusText}`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return await response.json();
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error(`Error fetching data from ${endpointUrl}:`, error);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async getUrlHistory(lastFetchTime?: string) {
|
|
40
|
+
const endpoint =
|
|
41
|
+
this.getEndpointUrl(API_ENDPOINT_URL_HISTORY) +
|
|
42
|
+
(lastFetchTime ? `?offset=${lastFetchTime}` : '');
|
|
43
|
+
return this.request(endpoint);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getSlugHistory(lastFetchTime?: string) {
|
|
47
|
+
const endpoint =
|
|
48
|
+
this.getEndpointUrl(API_ENDPOINT_SLUG_HISTORY) +
|
|
49
|
+
(lastFetchTime ? `?offset=${lastFetchTime}` : '');
|
|
50
|
+
return this.request(endpoint);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { MANAGEMENT_API_URL } from '../constants/endpoints';
|
|
2
|
+
import type { ManagementApiCredentials } from '@geins/types';
|
|
3
|
+
|
|
4
|
+
export class ManagementApiClient {
|
|
5
|
+
private baseUrl: string;
|
|
6
|
+
private apiKey: string;
|
|
7
|
+
private authToken: string;
|
|
8
|
+
|
|
9
|
+
constructor(credentials: ManagementApiCredentials) {
|
|
10
|
+
this.baseUrl = MANAGEMENT_API_URL;
|
|
11
|
+
this.apiKey = credentials.apiKey;
|
|
12
|
+
this.authToken = this.createAuthToken(credentials);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
createAuthToken(credentials: ManagementApiCredentials) {
|
|
16
|
+
return btoa(`${credentials.username}:${credentials.password}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getEndpointUrl(path: string) {
|
|
20
|
+
return `${this.baseUrl}/${path}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async request(method: string, path: string, data: any = {}) {
|
|
24
|
+
const endpointUrl = this.getEndpointUrl(path);
|
|
25
|
+
const options = {
|
|
26
|
+
method,
|
|
27
|
+
headers: {
|
|
28
|
+
Accept: 'application/json',
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
'x-apikey': this.apiKey,
|
|
31
|
+
Authorization: `Bearer ${this.authToken}`,
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify(data),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const reponse = await fetch(endpointUrl, options).then((response) => {
|
|
37
|
+
return response.json();
|
|
38
|
+
});
|
|
39
|
+
return reponse;
|
|
40
|
+
}
|
|
41
|
+
}
|