@ogcio/analytics-sdk 0.0.1-beta.1

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 (78) hide show
  1. package/dist/base/base-client.d.ts +36 -0
  2. package/dist/base/base-client.d.ts.map +1 -0
  3. package/dist/base/base-client.js +96 -0
  4. package/dist/index.d.ts +651 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +321 -0
  7. package/dist/matomo/helpers.d.ts +9 -0
  8. package/dist/matomo/helpers.d.ts.map +1 -0
  9. package/dist/matomo/helpers.js +10 -0
  10. package/dist/matomo/types.d.ts +33 -0
  11. package/dist/matomo/types.d.ts.map +1 -0
  12. package/dist/matomo/types.js +1 -0
  13. package/dist/ogcio-analytics-sdk-0.0.1.tgz +0 -0
  14. package/dist/package/dist/base/base-client.d.ts +36 -0
  15. package/dist/package/dist/base/base-client.d.ts.map +1 -0
  16. package/dist/package/dist/base/base-client.js +96 -0
  17. package/dist/package/dist/index.d.ts +651 -0
  18. package/dist/package/dist/index.d.ts.map +1 -0
  19. package/dist/package/dist/index.js +321 -0
  20. package/dist/package/dist/matomo/helpers.d.ts +9 -0
  21. package/dist/package/dist/matomo/helpers.d.ts.map +1 -0
  22. package/dist/package/dist/matomo/helpers.js +10 -0
  23. package/dist/package/dist/matomo/types.d.ts +33 -0
  24. package/dist/package/dist/matomo/types.d.ts.map +1 -0
  25. package/dist/package/dist/matomo/types.js +1 -0
  26. package/dist/package/dist/schema.d.ts +1295 -0
  27. package/dist/package/dist/schema.d.ts.map +1 -0
  28. package/dist/package/dist/schema.js +5 -0
  29. package/dist/package/dist/support-files/auth/index.d.ts +3 -0
  30. package/dist/package/dist/support-files/auth/index.d.ts.map +1 -0
  31. package/dist/package/dist/support-files/auth/index.js +76 -0
  32. package/dist/package/dist/support-files/auth/types.d.ts +31 -0
  33. package/dist/package/dist/support-files/auth/types.d.ts.map +1 -0
  34. package/dist/package/dist/support-files/auth/types.js +1 -0
  35. package/dist/package/dist/support-files/building-blocks-sdk.d.ts +17 -0
  36. package/dist/package/dist/support-files/building-blocks-sdk.d.ts.map +1 -0
  37. package/dist/package/dist/support-files/building-blocks-sdk.js +11 -0
  38. package/dist/package/dist/utils/index.d.ts +7 -0
  39. package/dist/package/dist/utils/index.d.ts.map +1 -0
  40. package/dist/package/dist/utils/index.js +19 -0
  41. package/dist/package/examples/environment.d.ts +11 -0
  42. package/dist/package/examples/index.ts +201 -0
  43. package/dist/package/examples/tsconfig.json +15 -0
  44. package/dist/package/package.json +56 -0
  45. package/dist/package/tsconfig.json +18 -0
  46. package/dist/package/types.d.ts +19 -0
  47. package/dist/schema.d.ts +1295 -0
  48. package/dist/schema.d.ts.map +1 -0
  49. package/dist/schema.js +5 -0
  50. package/dist/support-files/auth/index.d.ts +3 -0
  51. package/dist/support-files/auth/index.d.ts.map +1 -0
  52. package/dist/support-files/auth/index.js +76 -0
  53. package/dist/support-files/auth/types.d.ts +31 -0
  54. package/dist/support-files/auth/types.d.ts.map +1 -0
  55. package/dist/support-files/auth/types.js +1 -0
  56. package/dist/support-files/building-blocks-sdk.d.ts +17 -0
  57. package/dist/support-files/building-blocks-sdk.d.ts.map +1 -0
  58. package/dist/support-files/building-blocks-sdk.js +11 -0
  59. package/dist/utils/index.d.ts +7 -0
  60. package/dist/utils/index.d.ts.map +1 -0
  61. package/dist/utils/index.js +19 -0
  62. package/examples/environment.d.ts +11 -0
  63. package/examples/index.ts +201 -0
  64. package/examples/tsconfig.json +15 -0
  65. package/package.json +56 -0
  66. package/src/base/base-client.ts +172 -0
  67. package/src/index.test.ts +92 -0
  68. package/src/index.ts +449 -0
  69. package/src/matomo/helpers.ts +14 -0
  70. package/src/matomo/types.ts +48 -0
  71. package/src/schema.ts +1295 -0
  72. package/src/support-files/auth/index.ts +116 -0
  73. package/src/support-files/auth/types.ts +38 -0
  74. package/src/support-files/building-blocks-sdk.ts +32 -0
  75. package/src/utils/index.test.ts +46 -0
  76. package/src/utils/index.ts +20 -0
  77. package/tsconfig.json +18 -0
  78. package/types.d.ts +19 -0
@@ -0,0 +1,36 @@
1
+ import createClient, { type FetchResponse, ParseAsResponse } from "openapi-fetch";
2
+ import type { ErrorStatus, FilterKeys, OkStatus, ResponseContent, ResponseObjectMap } from "openapi-typescript-helpers";
3
+ export declare const ANALYTICS: "analytics";
4
+ export type SERVICE_NAME = typeof ANALYTICS;
5
+ export type TokenFunction = (serviceName: SERVICE_NAME) => Promise<string> | string;
6
+ export type SDKClientParams = {
7
+ baseUrl?: string;
8
+ };
9
+ export type BaseApiClientParams = SDKClientParams & {
10
+ getTokenFn?: TokenFunction;
11
+ };
12
+ export type DataResponseValue<T, O> = ParseAsResponse<FilterKeys<ResponseContent<FilterKeys<ResponseObjectMap<T>, OkStatus>>, "application/json">, O> & {
13
+ error: ParseAsResponse<FilterKeys<ResponseContent<FilterKeys<ResponseObjectMap<T>, ErrorStatus>>, "application/json">, O>;
14
+ };
15
+ /**
16
+ * This decorator is used to check if the baseUrl or token of the client are set.
17
+ * If the baseUrl or token are not set, it throws an error
18
+ */
19
+ export declare function needsBackend(target: unknown, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
20
+ declare abstract class BaseClient<T extends object> {
21
+ private baseUrl?;
22
+ private initialized;
23
+ protected token?: string;
24
+ protected getTokenFn?: TokenFunction;
25
+ protected serviceName: SERVICE_NAME | undefined;
26
+ protected client: ReturnType<typeof createClient<T>>;
27
+ constructor({ baseUrl, getTokenFn }: BaseApiClientParams);
28
+ deleteToken(): void;
29
+ protected getToken(): Promise<string | undefined>;
30
+ isInitialized(): boolean;
31
+ protected formatResponse<G extends Record<string, unknown>, O>(response: FetchResponse<G, O, "application/json">): DataResponseValue<G, O>;
32
+ protected formatError<G, O>(reason: unknown): DataResponseValue<G, O>;
33
+ protected checkBackend(): Promise<void>;
34
+ }
35
+ export default BaseClient;
36
+ //# sourceMappingURL=base-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-client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AAM,CAAC;IACzB,SAAS,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC;IACrC,SAAS,CAAC,WAAW,EAAE,YAAY,GAAG,SAAS,CAAC;IAEhD,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;gBAEzC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,mBAAmB;IA6BjD,WAAW;cAIF,QAAQ;IASjB,aAAa;IAIpB,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,EAC3D,QAAQ,EAAE,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,kBAAkB,CAAC,GAChD,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC;IA0B1B,SAAS,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,GAAG,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC;cAKrD,YAAY;CAe7B;AAED,eAAe,UAAU,CAAC"}
@@ -0,0 +1,96 @@
1
+ import createClient from "openapi-fetch";
2
+ export const ANALYTICS = "analytics";
3
+ /**
4
+ * This decorator is used to check if the baseUrl or token of the client are set.
5
+ * If the baseUrl or token are not set, it throws an error
6
+ */
7
+ export function needsBackend(target, propertyKey, descriptor) {
8
+ const originalMethod = descriptor.value;
9
+ descriptor.value = async function (...args) {
10
+ this.checkBackend();
11
+ return originalMethod.apply(this, args);
12
+ };
13
+ return descriptor;
14
+ }
15
+ class BaseClient {
16
+ baseUrl;
17
+ initialized;
18
+ token;
19
+ getTokenFn;
20
+ serviceName;
21
+ client;
22
+ constructor({ baseUrl, getTokenFn }) {
23
+ this.initialized = false;
24
+ if (baseUrl) {
25
+ this.baseUrl = baseUrl;
26
+ this.initialized = true;
27
+ }
28
+ if (getTokenFn) {
29
+ this.getTokenFn = getTokenFn;
30
+ }
31
+ this.token = undefined;
32
+ this.client = createClient({ baseUrl: this.baseUrl });
33
+ const authMiddleware = {
34
+ onRequest: async ({ request }) => {
35
+ if (!this.token && this.getTokenFn) {
36
+ this.token = await this.getTokenFn(this.serviceName);
37
+ }
38
+ if (this.token) {
39
+ request.headers.set("Authorization", `Bearer ${this.token}`);
40
+ }
41
+ return request;
42
+ },
43
+ };
44
+ this.client.use(authMiddleware);
45
+ }
46
+ deleteToken() {
47
+ this.token = undefined;
48
+ }
49
+ async getToken() {
50
+ if (this.getTokenFn) {
51
+ const token = await this.getTokenFn(this.serviceName);
52
+ this.token = token;
53
+ }
54
+ return this.token;
55
+ }
56
+ isInitialized() {
57
+ return this.initialized;
58
+ }
59
+ formatResponse(response) {
60
+ let outputData = undefined;
61
+ let outputMetadata = undefined;
62
+ if (!response) {
63
+ return {};
64
+ }
65
+ if (response.data) {
66
+ const dataEntries = Object.entries(response.data);
67
+ // by docs the body should contain a "data"
68
+ // properties with the response values
69
+ const containsData = dataEntries.find((x) => x[0] === "data");
70
+ const containsMetadata = dataEntries.find((x) => x[0] === "metadata");
71
+ if (containsMetadata) {
72
+ outputMetadata = containsMetadata[1];
73
+ }
74
+ outputData = containsData ? containsData[1] : response.data;
75
+ }
76
+ return {
77
+ data: outputData,
78
+ metadata: outputMetadata,
79
+ error: response.error,
80
+ };
81
+ }
82
+ formatError(reason) {
83
+ return { error: reason };
84
+ }
85
+ /* checkBackend is a protected method used by needsBackend decorator */
86
+ async checkBackend() {
87
+ const className = this.constructor.name;
88
+ if (!this.baseUrl) {
89
+ throw new Error(`${className} baseUrl is not set. Please provide a valid baseUrl when initializing ${className} SDK methods.`);
90
+ }
91
+ if (!this.token && !this.getTokenFn) {
92
+ throw new Error(`${className} token is not set. Please provide a valid getTokenFn function when initializing ${className} SDK methods.`);
93
+ }
94
+ }
95
+ }
96
+ export default BaseClient;