@remkoj/optimizely-graph-client 1.0.4 → 2.0.0-pre1

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 (57) hide show
  1. package/dist/admin-api/client/OptimizelyGraphAdminApi.js +1 -1
  2. package/dist/admin-api/client/core/OpenAPI.js +1 -1
  3. package/dist/admin-api/client/index.d.ts +1 -0
  4. package/dist/admin-api/client/index.js +1 -0
  5. package/dist/admin-api/client/index.js.map +1 -1
  6. package/dist/admin-api/client/models/DeleteMode.d.ts +5 -0
  7. package/dist/admin-api/client/models/DeleteMode.js +11 -0
  8. package/dist/admin-api/client/models/DeleteMode.js.map +1 -0
  9. package/dist/admin-api/client/models/PropertyType_V3.d.ts +2 -0
  10. package/dist/admin-api/client/services/DefinitionV3Service.d.ts +25 -11
  11. package/dist/admin-api/client/services/DefinitionV3Service.js +25 -12
  12. package/dist/admin-api/client/services/DefinitionV3Service.js.map +1 -1
  13. package/dist/admin-api/client/services/LogsService.d.ts +1 -1
  14. package/dist/admin-api/client/services/LogsService.js +1 -1
  15. package/dist/admin-api/client/services/ResourcesService.d.ts +20 -2
  16. package/dist/admin-api/client/services/ResourcesService.js +40 -2
  17. package/dist/admin-api/client/services/ResourcesService.js.map +1 -1
  18. package/dist/admin-api/client/services/WebhooksService.d.ts +10 -8
  19. package/dist/admin-api/client/services/WebhooksService.js +10 -8
  20. package/dist/admin-api/client/services/WebhooksService.js.map +1 -1
  21. package/dist/admin-api/request/request.js +0 -1
  22. package/dist/admin-api/request/request.js.map +1 -1
  23. package/dist/client/client.js +3 -1
  24. package/dist/client/client.js.map +1 -1
  25. package/dist/client/types.d.ts +21 -0
  26. package/dist/services/channel-repository/definition.d.ts +7 -1
  27. package/dist/services/channel-repository/definition.js +14 -0
  28. package/dist/services/channel-repository/definition.js.map +1 -1
  29. package/dist/services/channel-repository/repository.js +3 -3
  30. package/dist/services/channel-repository/repository.js.map +1 -1
  31. package/dist/services/channel-repository/types.d.ts +9 -5
  32. package/dist/services/index.d.ts +5 -3
  33. package/dist/services/index.js +2 -1
  34. package/dist/services/index.js.map +1 -1
  35. package/dist/services/routing/index.d.ts +4 -83
  36. package/dist/services/routing/index.js +2 -242
  37. package/dist/services/routing/index.js.map +1 -1
  38. package/dist/services/routing/queries/getAllRoutes.d.ts +28 -0
  39. package/dist/services/routing/queries/getAllRoutes.js +31 -0
  40. package/dist/services/routing/queries/getAllRoutes.js.map +1 -0
  41. package/dist/services/routing/queries/getRouteById.d.ts +13 -0
  42. package/dist/services/routing/queries/getRouteById.js +24 -0
  43. package/dist/services/routing/queries/getRouteById.js.map +1 -0
  44. package/dist/services/routing/queries/getRouteByPath.d.ts +12 -0
  45. package/dist/services/routing/queries/getRouteByPath.js +30 -0
  46. package/dist/services/routing/queries/getRouteByPath.js.map +1 -0
  47. package/dist/services/routing/resolver.d.ts +45 -0
  48. package/dist/services/routing/resolver.js +137 -0
  49. package/dist/services/routing/resolver.js.map +1 -0
  50. package/dist/services/routing/types.d.ts +3 -18
  51. package/dist/services/types.d.ts +14 -0
  52. package/dist/services/types.js +2 -0
  53. package/dist/services/types.js.map +1 -0
  54. package/dist/services/utils.d.ts +69 -0
  55. package/dist/services/utils.js +135 -0
  56. package/dist/services/utils.js.map +1 -0
  57. package/package.json +11 -6
@@ -0,0 +1,137 @@
1
+ // Import GraphQL Client
2
+ import createClient, { isContentGraphClient } from '../../client/index.js';
3
+ // Import GraphQL Queries
4
+ import * as GetRouteById from './queries/getRouteById.js';
5
+ import * as GetAllRoutes from './queries/getAllRoutes.js';
6
+ import * as GetRouteByPath from './queries/getRouteByPath.js';
7
+ // Main router class
8
+ /**
9
+ *
10
+ */
11
+ export class RouteResolver {
12
+ /**
13
+ * Create a new Route Resolver
14
+ *
15
+ * @param client ContentGraph configuration override
16
+ * @param apolloConfig Apollo Client configuration override
17
+ */
18
+ constructor(clientOrConfig) {
19
+ this._cgClient = isContentGraphClient(clientOrConfig) ? clientOrConfig : createClient(clientOrConfig);
20
+ }
21
+ /**
22
+ * Retrieve all registered routes for the provided domain - all domains if none specified
23
+ *
24
+ * @param domain The domain to filter on
25
+ * @returns The list of routes
26
+ */
27
+ async getRoutes(domain) {
28
+ this._cgClient.updateFlags({ queryCache: false }, true);
29
+ let page = await this._cgClient.request(GetAllRoutes.query, { domain }).catch(e => {
30
+ if (this._cgClient.debug)
31
+ console.error("[RouteResolver] Error while fetching routes", e);
32
+ return undefined;
33
+ });
34
+ let results = page?.Content?.items ?? [];
35
+ const totalCount = page?.Content?.total ?? 0;
36
+ const cursor = page?.Content?.cursor ?? '';
37
+ if (totalCount > 0 && cursor !== '' && totalCount > results.length)
38
+ while ((page?.Content?.items?.length ?? 0) > 0 && results.length < totalCount) {
39
+ page = await this._cgClient.request({
40
+ document: GetAllRoutes.query,
41
+ variables: {
42
+ cursor,
43
+ domain
44
+ }
45
+ });
46
+ results = results.concat(page.Content?.items ?? []);
47
+ }
48
+ this._cgClient.restoreFlags();
49
+ return results.map(this.tryConvertResponse.bind(this)).filter(this.isNotNullOrUndefined);
50
+ }
51
+ /**
52
+ * Retrieve route details by path
53
+ *
54
+ * @param path
55
+ * @param domain
56
+ * @returns The route information for the path
57
+ */
58
+ async getContentInfoByPath(path, domain) {
59
+ if (this._cgClient.debug)
60
+ console.log(`Resolving content info for ${path} on ${domain ? domain : "all domains"}`);
61
+ const resultSet = await this._cgClient.request({
62
+ document: GetRouteByPath.query,
63
+ variables: { path, domain }
64
+ });
65
+ if ((resultSet.Content?.items?.length ?? 0) === 0) {
66
+ if (this._cgClient.debug)
67
+ console.warn("No items in the resultset");
68
+ return undefined;
69
+ }
70
+ if ((resultSet.Content?.items?.length ?? 0) > 1)
71
+ throw new Error("Ambiguous URL provided, did you omit the siteId in a multi-channel setup?");
72
+ if (this._cgClient.debug)
73
+ console.log(`Resolved content info for ${path} to:`, resultSet.Content.items[0]);
74
+ return this.convertResponse(resultSet.Content.items[0]);
75
+ }
76
+ async getContentInfoById(key, locale, version) {
77
+ const variables = { key, version: version?.toString(), locale: locale?.replaceAll('-', '_') };
78
+ if (this._cgClient.debug)
79
+ console.log("Resolving content by id:", JSON.stringify(variables));
80
+ const resultSet = await this._cgClient.request({
81
+ document: GetRouteById.query,
82
+ variables
83
+ });
84
+ if (resultSet.Content?.total >= 1) {
85
+ if (this._cgClient.debug && resultSet.Content?.total > 1)
86
+ console.warn(`Received multiple entries with this ID, returning the first one from: ${(resultSet.Content?.items || []).map(x => { return `${x._metadata.key} (version: ${x._metadata.version}, locale: ${x._metadata.locale})`; }).join('; ')}`);
87
+ return this.convertResponse(resultSet.Content.items[0]);
88
+ }
89
+ return undefined;
90
+ }
91
+ /**
92
+ * Extract a content link from a route definition
93
+ *
94
+ * @param route The route to parse
95
+ * @returns The ContentLink, with locale information
96
+ */
97
+ routeToContentLink(route) {
98
+ return {
99
+ key: route.key,
100
+ version: route.version,
101
+ locale: route.locale
102
+ };
103
+ }
104
+ convertResponse(item) {
105
+ let itemUrl = new URL('http://localhost');
106
+ try {
107
+ itemUrl = new URL(item._metadata.url.path, item._metadata.url.domain);
108
+ }
109
+ catch (e) {
110
+ //Ignore
111
+ }
112
+ return {
113
+ locale: item._metadata.locale,
114
+ path: item._metadata.url.path,
115
+ url: itemUrl,
116
+ slug: "",
117
+ changed: item.changed ? new Date(item.changed) : null,
118
+ contentType: item._metadata.types,
119
+ version: item._metadata.version,
120
+ key: item._metadata.key
121
+ };
122
+ }
123
+ tryConvertResponse(item) {
124
+ try {
125
+ return this.convertResponse(item);
126
+ }
127
+ catch (e) {
128
+ console.error(`Unable to convert ${JSON.stringify(item)} to Route`, e);
129
+ return undefined;
130
+ }
131
+ }
132
+ isNotNullOrUndefined(input) {
133
+ return input != null && input != undefined;
134
+ }
135
+ }
136
+ export default RouteResolver;
137
+ //# sourceMappingURL=resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.js","sourceRoot":"","sources":["../../../src/services/routing/resolver.ts"],"names":[],"mappings":"AAAA,wBAA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S,CAAC,GAAG;SAC1B,CAAA;IACL,CAAC;IAES,kBAAkB,CAAC,IAAwB;QAEjD,IAAI,CAAC;YACD,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAA;QACrC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,OAAO,CAAC,KAAK,CAAC,qBAAsB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAE,WAAW,EAAE,CAAC,CAAC,CAAA;YACxE,OAAO,SAAS,CAAA;QACpB,CAAC;IACL,CAAC;IAES,oBAAoB,CAAI,KAA2B;QAEzD,OAAO,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,SAAS,CAAA;IAC9C,CAAC;CACJ;AAED,eAAe,aAAa,CAAA"}
@@ -1,25 +1,10 @@
1
- export type ContentLink = {
2
- id: number;
3
- workId?: number | null;
4
- guidValue?: string | null;
5
- } | {
6
- id?: number | null;
7
- workId?: number | null;
8
- guidValue: string;
9
- };
10
- export type ContentLinkWithLocale = ContentLink & {
11
- locale?: string;
12
- };
13
1
  export type Route = {
14
- language: string;
2
+ locale: string;
15
3
  path: string;
16
4
  url: URL;
17
5
  slug: string;
18
6
  changed: Date | null;
19
- published: Date | null;
20
7
  contentType: string[];
21
- id: number;
22
- workId?: number | null;
23
- guid: string;
24
- siteId: string;
8
+ version?: string | null;
9
+ key: string;
25
10
  };
@@ -0,0 +1,14 @@
1
+ export type ContentLink = {
2
+ key: string;
3
+ version?: string | null;
4
+ };
5
+ export type InlineContentLink = {
6
+ key?: null | undefined;
7
+ version?: null;
8
+ };
9
+ export type ContentLinkWithLocale<LocaleType = string> = ContentLink & {
10
+ locale?: LocaleType;
11
+ };
12
+ export type InlineContentLinkWithLocale<LocaleType = string> = InlineContentLink & {
13
+ locale?: LocaleType;
14
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/services/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,69 @@
1
+ import type { ContentLink, ContentLinkWithLocale, InlineContentLink, InlineContentLinkWithLocale } from "./types.js";
2
+ /**
3
+ * Transform a locale code (e.g. en-US) to a Optimizely Graph compatible locale
4
+ * (e.g. en_US).
5
+ *
6
+ * @param locale The locale to transform
7
+ * @returns The Optimizely Graph safe locale
8
+ */
9
+ export declare function localeToGraphLocale<T>(locale: T): T extends string ? string : never;
10
+ /**
11
+ * Tranform an Optimizely Graph locale (e.g. en_US) to an ISO locale (e.g. en-US)
12
+ *
13
+ * @param graphLocale The locale as supported by Optimizely Graph
14
+ * @returns The ISO Locale
15
+ */
16
+ export declare function graphLocaleToLocale<T>(graphLocale: T): T extends string ? string : never;
17
+ /**
18
+ * Check if two ContentLinks point to the same content item, if either of the
19
+ * links set the version or locale, these values must be equal across the ContentLinks
20
+ *
21
+ * @param link1 The first compared link
22
+ * @param link2 The second compared link
23
+ * @returns True if they links point to the same content item, False otherwise
24
+ */
25
+ export declare function contentLinkIsEqual(link1: ContentLink | ContentLinkWithLocale, link2: ContentLink | ContentLinkWithLocale): boolean;
26
+ /**
27
+ * Check if two ContentLinks point to the same content item, if either of the
28
+ * links set the version or locale, these values must be equal across the ContentLinks
29
+ *
30
+ * @param link1 The first compared link
31
+ * @param link2 The second compared link
32
+ * @returns True if they links point to the same content item, False otherwise
33
+ */
34
+ export declare function contentLinkIsEqualIgnoreVersion(link1: ContentLink | ContentLinkWithLocale, link2: ContentLink | ContentLinkWithLocale): boolean;
35
+ /**
36
+ * Test if the provided value can be understood as a ContentLink
37
+ *
38
+ * @param toTest The value under test
39
+ * @returns True if the object can be understood as a ContentLink, False otherwise
40
+ */
41
+ export declare function isContentLink(toTest: any): toTest is ContentLink;
42
+ export declare function isContentLinkWithLocale(toTest: any): toTest is ContentLinkWithLocale;
43
+ /**
44
+ * Test if the variable is an
45
+ *
46
+ * @param toTest
47
+ * @returns
48
+ */
49
+ export declare function isInlineContentLink(toTest: any): toTest is InlineContentLinkWithLocale;
50
+ type Nullable<T> = {
51
+ [K in keyof T]?: T[K] | null;
52
+ } | null | undefined;
53
+ /**
54
+ * Take the normalizable value
55
+ *
56
+ * @param toNormalize
57
+ * @returns
58
+ */
59
+ export declare function normalizeContentLink(toNormalize: Nullable<ContentLink | InlineContentLink>): ContentLink | InlineContentLink | undefined;
60
+ export declare function normalizeContentLinkWithLocale<LT = string>(toNormalize: Nullable<ContentLinkWithLocale<LT>>): ContentLinkWithLocale<LT> | undefined;
61
+ /**
62
+ * Create a textual representation of a content link
63
+ *
64
+ * @param contentLink The (inline) content link to transform to a string
65
+ * @param unique Set to true to ensure that this method returns a string that can be used as key
66
+ * @returns The textual representation of the ContentLink
67
+ */
68
+ export declare function contentLinkToString(contentLink: ContentLink | InlineContentLink | null | undefined, unique?: boolean): string;
69
+ export {};
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Transform a locale code (e.g. en-US) to a Optimizely Graph compatible locale
3
+ * (e.g. en_US).
4
+ *
5
+ * @param locale The locale to transform
6
+ * @returns The Optimizely Graph safe locale
7
+ */
8
+ export function localeToGraphLocale(locale) {
9
+ if (typeof (locale) != 'string' || (locale.length != 2 && locale.length != 5))
10
+ throw new Error(`The value ${locale} is not a supported ISO locale`);
11
+ return locale.replaceAll('-', '_');
12
+ }
13
+ /**
14
+ * Tranform an Optimizely Graph locale (e.g. en_US) to an ISO locale (e.g. en-US)
15
+ *
16
+ * @param graphLocale The locale as supported by Optimizely Graph
17
+ * @returns The ISO Locale
18
+ */
19
+ export function graphLocaleToLocale(graphLocale) {
20
+ if (typeof (graphLocale) != 'string' || (graphLocale.length != 2 && graphLocale.length != 5))
21
+ throw new Error(`The value ${graphLocale} is not a supported ISO locale`);
22
+ return graphLocale.replaceAll('_', '-');
23
+ }
24
+ /**
25
+ * Check if two ContentLinks point to the same content item, if either of the
26
+ * links set the version or locale, these values must be equal across the ContentLinks
27
+ *
28
+ * @param link1 The first compared link
29
+ * @param link2 The second compared link
30
+ * @returns True if they links point to the same content item, False otherwise
31
+ */
32
+ export function contentLinkIsEqual(link1, link2) {
33
+ if (link1.key != link2.key)
34
+ return false;
35
+ if ((link1.version || link2.version) && link1.version != link2.version)
36
+ return false;
37
+ if ((link1.locale || link2.locale) && link1.locale != link2.locale)
38
+ return false;
39
+ return true;
40
+ }
41
+ /**
42
+ * Check if two ContentLinks point to the same content item, if either of the
43
+ * links set the version or locale, these values must be equal across the ContentLinks
44
+ *
45
+ * @param link1 The first compared link
46
+ * @param link2 The second compared link
47
+ * @returns True if they links point to the same content item, False otherwise
48
+ */
49
+ export function contentLinkIsEqualIgnoreVersion(link1, link2) {
50
+ if (link1.key != link2.key)
51
+ return false;
52
+ if ((link1.locale || link2.locale) && link1.locale != link2.locale)
53
+ return false;
54
+ return true;
55
+ }
56
+ /**
57
+ * Test if the provided value can be understood as a ContentLink
58
+ *
59
+ * @param toTest The value under test
60
+ * @returns True if the object can be understood as a ContentLink, False otherwise
61
+ */
62
+ export function isContentLink(toTest) {
63
+ // It must be an object
64
+ if (typeof (toTest) != 'object' || toTest == null)
65
+ return false;
66
+ // The object must have a key, string, miniumum length 1 char
67
+ return typeof (toTest.key) == 'string' && toTest.key.length > 0;
68
+ }
69
+ export function isContentLinkWithLocale(toTest) {
70
+ if (!isContentLink(toTest))
71
+ return false;
72
+ const locale = toTest.locale;
73
+ return locale == undefined || locale == null || (typeof (locale) == 'string' && locale.length >= 2 && locale.length <= 5);
74
+ }
75
+ /**
76
+ * Test if the variable is an
77
+ *
78
+ * @param toTest
79
+ * @returns
80
+ */
81
+ export function isInlineContentLink(toTest) {
82
+ if (typeof toTest != 'object' || toTest == null)
83
+ return false;
84
+ return toTest.key == null &&
85
+ (toTest.version == null || toTest.version == undefined) &&
86
+ (typeof toTest.locale == 'string' || toTest.locale == null || toTest.locale == undefined);
87
+ }
88
+ /**
89
+ * Take the normalizable value
90
+ *
91
+ * @param toNormalize
92
+ * @returns
93
+ */
94
+ export function normalizeContentLink(toNormalize) {
95
+ if (!(isContentLink(toNormalize) || isInlineContentLink(toNormalize)))
96
+ return undefined;
97
+ const newLink = {
98
+ key: toNormalize.key
99
+ };
100
+ if (toNormalize.version)
101
+ newLink.version = toNormalize.version.toString();
102
+ return toNormalize;
103
+ }
104
+ export function normalizeContentLinkWithLocale(toNormalize) {
105
+ const normalized = normalizeContentLink(toNormalize);
106
+ if (toNormalize?.locale != undefined && !(typeof (toNormalize?.locale) == 'string' && (toNormalize.locale.length == 2 || toNormalize.locale.length == 5)))
107
+ normalized.locale = toNormalize.locale;
108
+ return normalized;
109
+ }
110
+ function generateUniqueKey(prefix = "inline::") {
111
+ try {
112
+ return prefix + crypto.randomUUID().replaceAll('-', '');
113
+ }
114
+ catch (e) {
115
+ console.warn("💥 Crypto library unavailable, expect key collisions");
116
+ return prefix + 'default';
117
+ }
118
+ }
119
+ /**
120
+ * Create a textual representation of a content link
121
+ *
122
+ * @param contentLink The (inline) content link to transform to a string
123
+ * @param unique Set to true to ensure that this method returns a string that can be used as key
124
+ * @returns The textual representation of the ContentLink
125
+ */
126
+ export function contentLinkToString(contentLink, unique = false) {
127
+ if (!contentLink)
128
+ return unique ? generateUniqueKey('no-content::') : '[no content]';
129
+ return [
130
+ contentLink.key == null ? (unique ? generateUniqueKey() : "[inline content]") : contentLink.key,
131
+ contentLink.version,
132
+ contentLink.locale
133
+ ].filter(x => x).join('::');
134
+ }
135
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/services/utils.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAI,MAAS;IAE5C,IAAI,OAAM,CAAC,MAAM,CAAC,IAAI,QAAQ,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;QACxE,MAAM,IAAI,KAAK,CAAC,aAAc,MAAO,gCAAgC,CAAC,CAAA;IAC1E,OAAO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CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG;QAC/F,WAAW,CAAC,OAAO;QAClB,WAAqC,CAAC,MAAM;KAChD,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AAC/B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@remkoj/optimizely-graph-client",
3
3
  "license": "Apache-2.0",
4
- "version": "1.0.4",
4
+ "version": "2.0.0-pre1",
5
5
  "packageManager": "yarn@4.1.1",
6
6
  "repository": "https://github.com/remkoj/optimizely-dxp-clients.git",
7
7
  "author": "Remko Jantzen <693172+remkoj@users.noreply.github.com>",
@@ -19,7 +19,8 @@
19
19
  "./router": "./dist/services/routing/index.js",
20
20
  "./channels": "./dist/services/channel-repository/index.js",
21
21
  "./admin": "./dist/admin-api/index.js",
22
- "./codegen": "./dist/codegen.js"
22
+ "./codegen": "./dist/codegen.js",
23
+ "./utils": "./dist/services/utils.js"
23
24
  },
24
25
  "typesVersions": {
25
26
  "*": {
@@ -43,18 +44,21 @@
43
44
  ],
44
45
  "codegen": [
45
46
  "dist/codegen.d.ts"
47
+ ],
48
+ "utils": [
49
+ "./dist/services/utils.d.ts"
46
50
  ]
47
51
  }
48
52
  },
49
53
  "devDependencies": {
50
54
  "@types/crypto-js": "^4.2.2",
51
- "@types/node": "^20.12.6",
55
+ "@types/node": "^20.12.7",
52
56
  "graphql": "^16.8.1",
53
57
  "graphql-request": "^6.1.0",
54
58
  "openapi-typescript-codegen": "^0.29.0",
55
59
  "prop-types": "^15.8.1",
56
60
  "scheduler": "^0.23.0",
57
- "typescript": "^5.4.4"
61
+ "typescript": "^5.4.5"
58
62
  },
59
63
  "dependencies": {
60
64
  "crypto-js": "^4.2.0"
@@ -66,8 +70,9 @@
66
70
  "clean": "tsc --build --clean",
67
71
  "compile-ts": "tsc --build",
68
72
  "prepare": "openapi --input https://cg.optimizely.com/app/swagger/swagger.json --output ./src/admin-api/client --name OptimizelyGraphAdminApi && node ./build/update-imports.cjs && tsc --build",
69
- "watch": "tsc --watch",
73
+ "watch": "yarn tsc --watch",
70
74
  "recompile": "tsc --build --clean && tsc --build --force",
71
75
  "generate-admin": "openapi --input https://cg.optimizely.com/app/swagger/swagger.json --output ./src/admin-api/client --name OptimizelyGraphAdminApi && node ./build/update-imports.cjs"
72
- }
76
+ },
77
+ "stableVersion": "1.0.4"
73
78
  }