@nascentdigital/funnel-cms 5.0.1 → 5.0.3

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 (51) hide show
  1. package/dist/contentful/ContentfulAccess.d.ts +19 -0
  2. package/dist/contentful/ContentfulAccess.js +67 -0
  3. package/dist/contentful/ContentfulApiCommand.d.ts +21 -0
  4. package/dist/contentful/ContentfulApiCommand.js +20 -0
  5. package/dist/contentful/ContentfulApiResponse.d.ts +10 -0
  6. package/dist/contentful/ContentfulApiResponse.js +2 -0
  7. package/dist/contentful/ContentfulAssetClient.d.ts +11 -0
  8. package/dist/contentful/ContentfulAssetClient.js +26 -0
  9. package/dist/contentful/ContentfulAuthClient.d.ts +9 -0
  10. package/dist/contentful/ContentfulAuthClient.js +20 -0
  11. package/dist/contentful/ContentfulChangeSetClient.d.ts +14 -0
  12. package/dist/contentful/ContentfulChangeSetClient.js +35 -0
  13. package/dist/contentful/ContentfulCollectionClient.d.ts +38 -0
  14. package/dist/contentful/ContentfulCollectionClient.js +173 -0
  15. package/dist/contentful/ContentfulContentClient.d.ts +60 -0
  16. package/dist/contentful/ContentfulContentClient.js +272 -0
  17. package/dist/contentful/ContentfulContentClientFactory.d.ts +12 -0
  18. package/dist/contentful/ContentfulContentClientFactory.js +19 -0
  19. package/dist/contentful/ContentfulPageClient.d.ts +43 -0
  20. package/dist/contentful/ContentfulPageClient.js +459 -0
  21. package/dist/contentful/ContentfulSiteConfigClient.d.ts +9 -0
  22. package/dist/contentful/ContentfulSiteConfigClient.js +33 -0
  23. package/dist/contentful/ContentfulUpgradeClient.d.ts +9 -0
  24. package/dist/contentful/ContentfulUpgradeClient.js +17 -0
  25. package/dist/contentful/ContentfulUserClient.d.ts +13 -0
  26. package/dist/contentful/ContentfulUserClient.js +32 -0
  27. package/dist/contentful/IContentfulContentClient.d.ts +61 -0
  28. package/dist/contentful/IContentfulContentClient.js +2 -0
  29. package/dist/contentful/api/ContentfulApi.d.ts +10 -0
  30. package/dist/contentful/api/ContentfulApi.js +82 -0
  31. package/dist/contentful/index.d.ts +12 -0
  32. package/dist/contentful/index.js +28 -0
  33. package/dist/contentful/models/ContentfulAssetData.d.ts +10 -0
  34. package/dist/contentful/models/ContentfulAssetData.js +29 -0
  35. package/dist/contentful/models/ContentfulCollectionData.d.ts +13 -0
  36. package/dist/contentful/models/ContentfulCollectionData.js +98 -0
  37. package/dist/contentful/models/ContentfulData.d.ts +39 -0
  38. package/dist/contentful/models/ContentfulData.js +118 -0
  39. package/dist/contentful/models/ContentfulPageData.d.ts +19 -0
  40. package/dist/contentful/models/ContentfulPageData.js +117 -0
  41. package/dist/contentful/models/ContentfulSiteConfigData.d.ts +10 -0
  42. package/dist/contentful/models/ContentfulSiteConfigData.js +9 -0
  43. package/dist/contentful/models/ContentfulUserData.d.ts +6 -0
  44. package/dist/contentful/models/ContentfulUserData.js +26 -0
  45. package/dist/contentful/models/index.d.ts +6 -0
  46. package/dist/contentful/models/index.js +22 -0
  47. package/dist/contentful/server.d.ts +1 -0
  48. package/dist/contentful/server.js +17 -0
  49. package/dist/models/Pagination.d.ts +1 -0
  50. package/dist/strapi/StrapiPageClient.js +11 -10
  51. package/package.json +9 -3
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.ContentfulContentClient = void 0;
15
+ const funnel_core_1 = require("@nascentdigital/funnel-core");
16
+ const contentful_management_1 = require("contentful-management");
17
+ const models_1 = require("../models");
18
+ const ContentfulAccess_1 = require("./ContentfulAccess");
19
+ const ContentfulAssetClient_1 = require("./ContentfulAssetClient");
20
+ const ContentfulAuthClient_1 = require("./ContentfulAuthClient");
21
+ const ContentfulChangeSetClient_1 = require("./ContentfulChangeSetClient");
22
+ const ContentfulCollectionClient_1 = require("./ContentfulCollectionClient");
23
+ const ContentfulPageClient_1 = require("./ContentfulPageClient");
24
+ const ContentfulSiteConfigClient_1 = require("./ContentfulSiteConfigClient");
25
+ const ContentfulUpgradeClient_1 = require("./ContentfulUpgradeClient");
26
+ const ContentfulUserClient_1 = require("./ContentfulUserClient");
27
+ const models_2 = require("./models");
28
+ // class
29
+ class ContentfulContentClient {
30
+ constructor(args) {
31
+ var _a;
32
+ // initialize instance variables
33
+ let sp;
34
+ if (ContentfulContentClient.ConstructorArgs.isForServer(args)) {
35
+ this.user = models_1.User.UNAUTHENTICATED;
36
+ this.locale = args.locale;
37
+ const options = { defaults: { spaceId: args.spaceId, environmentId: args.environment } };
38
+ this.cma = (0, contentful_management_1.createClient)({ accessToken: args.accessToken }, options);
39
+ this.cda = (0, contentful_management_1.createClient)({ accessToken: args.deliveryAccessToken, host: 'cdn.contentful.com' }, options);
40
+ this.cpa = (0, contentful_management_1.createClient)({ accessToken: args.previewAccessToken, host: 'preview.contentful.com' }, options);
41
+ }
42
+ else {
43
+ const { user, locales, cma, ids } = args.sdk;
44
+ this.user = new models_1.User({
45
+ brands: [funnel_core_1.AppConfig.brand()],
46
+ id: user.sys.id,
47
+ email: user.email,
48
+ username: user.email,
49
+ blocked: false,
50
+ role: user.spaceMembership.roles.length === 0
51
+ ? 'public'
52
+ : user.spaceMembership.roles
53
+ .map(({ name }) => models_2.ContentfulUserData.parseRole(name))
54
+ .sort((lhs, rhs) => lhs.compareTo(rhs))[0].id
55
+ });
56
+ this.locale = locales.default;
57
+ this.cma = cma;
58
+ const options = { defaults: { spaceId: ids.space, environmentId: (_a = ids.environmentAlias) !== null && _a !== void 0 ? _a : ids.environment } };
59
+ this.cda = (0, contentful_management_1.createClient)({ accessToken: args.deliveryAccessToken, host: 'cdn.contentful.com' }, options);
60
+ this.cpa = (0, contentful_management_1.createClient)({ accessToken: args.previewAccessToken, host: 'preview.contentful.com' }, options);
61
+ }
62
+ this.deliveryAccessToken = args.deliveryAccessToken;
63
+ this.previewAccessToken = args.previewAccessToken;
64
+ this.auth = new ContentfulAuthClient_1.ContentfulAuthClient(this);
65
+ this.changeSets = new ContentfulChangeSetClient_1.ContentfulChangeSetClient(this);
66
+ this.files = new ContentfulAssetClient_1.ContentfulAssetClient(this);
67
+ this.pages = new ContentfulPageClient_1.ContentfulPageClient(this);
68
+ this.collections = new ContentfulCollectionClient_1.ContentfulCollectionClient(this);
69
+ this.upgrade = new ContentfulUpgradeClient_1.ContentfulUpgradeClient(this);
70
+ this.users = new ContentfulUserClient_1.ContentfulUserClient(this);
71
+ this.siteConfig = new ContentfulSiteConfigClient_1.ContentfulSiteConfigClient(this);
72
+ }
73
+ get token() {
74
+ throw new funnel_core_1.NotImplementedError();
75
+ }
76
+ set token(token) {
77
+ throw new funnel_core_1.NotImplementedError();
78
+ }
79
+ clearToken() {
80
+ throw new funnel_core_1.NotImplementedError();
81
+ }
82
+ async createEntry(contentTypeId, entry) {
83
+ // create entry
84
+ const { locale, cma } = this;
85
+ const data = await cma.entry.create({ contentTypeId }, {
86
+ fields: models_2.ContentfulData.toEntryData(entry, locale)
87
+ });
88
+ // transform + return new entry
89
+ return models_2.ContentfulData.fromCmaData(data, locale);
90
+ }
91
+ async getEntry({ entryId, includeLinks = false, version = 'draft' }) {
92
+ var _a, _b;
93
+ // get entry
94
+ const client = version === 'published' ? this.cda : this.cpa;
95
+ const result = await client.entry.getMany({
96
+ query: {
97
+ 'sys.id': entryId,
98
+ include: includeLinks === true
99
+ ? 10
100
+ : includeLinks || undefined
101
+ }
102
+ });
103
+ const { items: [item], includes } = result;
104
+ console.debug('[ContentfulContentClient/getEntry] fetched', result);
105
+ // bind references
106
+ const entry = models_2.ContentfulData.fromCdaData(item);
107
+ const references = (_b = (_a = includes === null || includes === void 0 ? void 0 : includes.Entry) === null || _a === void 0 ? void 0 : _a.map(link => models_2.ContentfulData.fromCdaData(link))) !== null && _b !== void 0 ? _b : [];
108
+ // return normalized item
109
+ return { entry, references };
110
+ }
111
+ async getEntries(_a) {
112
+ var _b, _c;
113
+ var { pagination = 'all', version = 'draft', includeLinks = false } = _a, query = __rest(_a, ["pagination", "version", "includeLinks"]);
114
+ // setup pagination
115
+ const itemsPerRequest = pagination === 'all' ? 1000 : Math.min(pagination.pageSize, 1000);
116
+ const page = pagination === 'all' ? 1 : pagination.page;
117
+ let skip = pagination === 'all' ? 0 : (page - 1) * pagination.pageSize;
118
+ let totalItemFetched = 0;
119
+ let hasMorePages = true;
120
+ let totalItems = undefined;
121
+ // loop until no more pages
122
+ const client = version === 'published' ? this.cda : this.cpa;
123
+ const entries = [];
124
+ const includes = [];
125
+ do {
126
+ // fetch next page
127
+ const _d = await client.entry.getMany({
128
+ query: Object.assign(Object.assign({}, query), { limit: itemsPerRequest, skip, include: includeLinks === true
129
+ ? 10
130
+ : includeLinks || undefined })
131
+ }), { items } = _d, response = __rest(_d, ["items"]);
132
+ console.debug('[ContentfulContentClient/getEntries] fetched page', { query, items, response });
133
+ // add items
134
+ items.forEach(item => entries.push(models_2.ContentfulData.fromCdaData(item)));
135
+ includes.push(...((_c = (_b = response.includes) === null || _b === void 0 ? void 0 : _b.Entry) !== null && _c !== void 0 ? _c : []));
136
+ // update paging info
137
+ totalItemFetched += items.length;
138
+ totalItems = response.total;
139
+ skip += items.length;
140
+ hasMorePages = pagination === 'all'
141
+ ? skip < totalItems
142
+ : totalItemFetched < pagination.pageSize && skip < totalItems;
143
+ } while (hasMorePages);
144
+ console.debug('[ContentfulContentClient/getEntries] fetched entries', { entries, includes });
145
+ // normalize references
146
+ const references = includes.map(link => models_2.ContentfulData.fromCdaData(link));
147
+ // return
148
+ return {
149
+ entries,
150
+ references,
151
+ page,
152
+ pageCount: pagination === 'all'
153
+ ? 1
154
+ : Math.floor(totalItems / pagination.pageSize),
155
+ totalItemCount: totalItems
156
+ };
157
+ }
158
+ async uploadAsset(args) {
159
+ var _a;
160
+ // upload file
161
+ const { cma, locale } = this;
162
+ const uploadResult = await this.cma.upload.create({}, {
163
+ file: await args.file.arrayBuffer()
164
+ });
165
+ // create contentful asset
166
+ const contentfulAsset = await cma.asset.create({}, {
167
+ fields: models_2.ContentfulData.toEntryData({
168
+ title: args.name,
169
+ description: (_a = args.description) !== null && _a !== void 0 ? _a : '',
170
+ file: {
171
+ contentType: args.file.type,
172
+ fileName: args.name,
173
+ uploadFrom: {
174
+ sys: {
175
+ type: 'Link',
176
+ linkType: 'Upload',
177
+ id: uploadResult.sys.id
178
+ }
179
+ }
180
+ }
181
+ }, locale)
182
+ });
183
+ // process contentful asset (creates url etc.) and return
184
+ const processedAsset = await cma.asset.processForAllLocales({}, contentfulAsset);
185
+ // create funnel asset then return
186
+ const entry = await cma.entry.create({ contentTypeId: models_2.ContentfulAssetData.contentType }, {
187
+ fields: models_2.ContentfulData.toEntryData({
188
+ name: args.name,
189
+ description: args.description,
190
+ brand: args.brand,
191
+ type: args.type,
192
+ scope: args.scope,
193
+ tags: args.tags,
194
+ url: 'https:' + processedAsset.fields.file[locale].url,
195
+ size: args.size,
196
+ metadata: args.metadata,
197
+ media: {
198
+ sys: {
199
+ type: 'Link',
200
+ linkType: 'Asset',
201
+ id: processedAsset.sys.id
202
+ }
203
+ }
204
+ }, locale)
205
+ });
206
+ // return asset
207
+ return models_2.ContentfulData.fromCmaData(entry, locale);
208
+ }
209
+ static async forEditor(sdk) {
210
+ var _a;
211
+ // get current site config (throw if we can't)
212
+ const { user, cma, locales: { default: locale } } = sdk;
213
+ const userId = user.sys.id;
214
+ const brand = funnel_core_1.AppConfig.brand();
215
+ const { items: [siteConfigData] } = await cma.entry.getMany({
216
+ query: {
217
+ content_type: models_2.ContentfulSiteConfigData.contentType,
218
+ 'fields.brand': brand
219
+ }
220
+ });
221
+ const siteConfig = siteConfigData && models_2.ContentfulData.fromCmaData(siteConfigData, locale);
222
+ if (!siteConfig) {
223
+ throw new funnel_core_1.NotFoundError('Unable to resolve SiteConfig');
224
+ }
225
+ // grant user temp access via the new token
226
+ const access = ContentfulAccess_1.ContentfulAccess.new(userId);
227
+ await cma.entry.patch({ entryId: siteConfig.id, version: siteConfig.__version }, [
228
+ {
229
+ op: 'replace',
230
+ path: `/fields/access/${locale}`,
231
+ value: Object.assign(Object.assign({}, siteConfig.access), { [userId]: access.data })
232
+ }
233
+ ]);
234
+ // call API to get delivery/preview tokens
235
+ const command = {
236
+ type: 'GetAccessTokens',
237
+ userId: access.userId,
238
+ token: access.token
239
+ };
240
+ const response = await fetch('/api/contentful', {
241
+ method: 'POST',
242
+ body: JSON.stringify(command)
243
+ });
244
+ // throw if response failed
245
+ await funnel_core_1.IsometricErrorUtils.checkResponse(response);
246
+ // throw if not valid
247
+ const { data } = (_a = await response.json()) !== null && _a !== void 0 ? _a : {};
248
+ const { deliveryAccessToken, previewAccessToken } = data !== null && data !== void 0 ? data : {};
249
+ if (!deliveryAccessToken || !previewAccessToken) {
250
+ throw new funnel_core_1.IllegalStateError('Invalid data return from API');
251
+ }
252
+ console.debug('[ContentfulContentClient/forEditor] creating with', { sdk, deliveryAccessToken, previewAccessToken });
253
+ // create + return new client
254
+ return new ContentfulContentClient({ sdk, deliveryAccessToken, previewAccessToken });
255
+ }
256
+ }
257
+ exports.ContentfulContentClient = ContentfulContentClient;
258
+ // extension
259
+ (function (ContentfulContentClient) {
260
+ let ConstructorArgs;
261
+ (function (ConstructorArgs) {
262
+ // helpers
263
+ function isForServer(args) {
264
+ return 'accessToken' in args;
265
+ }
266
+ ConstructorArgs.isForServer = isForServer;
267
+ function isForEditor(args) {
268
+ return 'sdk' in args;
269
+ }
270
+ ConstructorArgs.isForEditor = isForEditor;
271
+ })(ConstructorArgs = ContentfulContentClient.ConstructorArgs || (ContentfulContentClient.ConstructorArgs = {}));
272
+ })(ContentfulContentClient || (exports.ContentfulContentClient = ContentfulContentClient = {}));
@@ -0,0 +1,12 @@
1
+ import { ContentClient } from '../api';
2
+ import { ContentfulContentClient } from './ContentfulContentClient';
3
+ export declare class ContentfulContentClientFactory implements ContentClient.Factory {
4
+ private readonly args;
5
+ private readonly _serverClient;
6
+ constructor(args: ContentfulContentClientFactory.ConstructorArgs);
7
+ forServer(): ContentfulContentClient;
8
+ forEditor(): ContentfulContentClient;
9
+ }
10
+ export declare namespace ContentfulContentClientFactory {
11
+ type ConstructorArgs = ContentfulContentClient.ConstructorArgs;
12
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContentfulContentClientFactory = void 0;
4
+ const ContentfulContentClient_1 = require("./ContentfulContentClient");
5
+ // factory
6
+ class ContentfulContentClientFactory {
7
+ constructor(args) {
8
+ this.args = args;
9
+ // initialize instance variables
10
+ this._serverClient = new ContentfulContentClient_1.ContentfulContentClient(args);
11
+ }
12
+ forServer() {
13
+ return this._serverClient;
14
+ }
15
+ forEditor() {
16
+ return new ContentfulContentClient_1.ContentfulContentClient(this.args);
17
+ }
18
+ }
19
+ exports.ContentfulContentClientFactory = ContentfulContentClientFactory;
@@ -0,0 +1,43 @@
1
+ import { Schema } from '@nascentdigital/funnel-core';
2
+ import { PageClient } from '../api';
3
+ import { IContentfulContentClient } from './IContentfulContentClient';
4
+ import { ContentfulPageData } from './models';
5
+ export declare class ContentfulPageClient implements PageClient {
6
+ readonly cms: IContentfulContentClient;
7
+ constructor(cms: IContentfulContentClient);
8
+ getOverview(args: PageClient.GetOverview.Args): Promise<PageClient.GetOverview.Result>;
9
+ getSummaries(args: PageClient.GetSummaries.Args): Promise<PageClient.GetSummaries.Result>;
10
+ getPreviews(args: PageClient.GetPreviews.Args): Promise<PageClient.GetPreviews.Result>;
11
+ search(args: PageClient.Search.Args): Promise<PageClient.Search.Result>;
12
+ query(args: PageClient.Query.Args): Promise<PageClient.Query.Result>;
13
+ find(args: PageClient.Find.Args): Promise<PageClient.Find.Result>;
14
+ getGroup(args: PageClient.GetGroup.Args): Promise<ContentfulPageClient.GetGroup.Result>;
15
+ get(args: PageClient.Get.Args): Promise<ContentfulPageClient.Get.Result>;
16
+ create(args: PageClient.Create.Args): Promise<PageClient.Create.Result>;
17
+ duplicate(args: PageClient.Duplicate.Args): Promise<PageClient.Duplicate.Result>;
18
+ update(args: PageClient.Update.Args): Promise<ContentfulPageClient.Update.Result>;
19
+ replace(args: PageClient.Replace.Args): Promise<PageClient.Replace.Result>;
20
+ touch(args: PageClient.Touch.Args): Promise<PageClient.Touch.Result>;
21
+ unpublish(args: PageClient.Unpublish.Args): Promise<PageClient.Unpublish.Result>;
22
+ getDependants(args: PageClient.GetDependants.Args): Promise<PageClient.GetDependants.Result>;
23
+ getDependencies(args: PageClient.GetDependencies.Args): Promise<PageClient.GetDependencies.Result>;
24
+ validateSlug(args: PageClient.ValidateSlug.Args): Promise<PageClient.ValidateSlug.Result>;
25
+ }
26
+ export declare namespace ContentfulPageClient {
27
+ type FlattedPageData = Omit<Schema.Page.FlattenedData, 'data'> & {
28
+ data: ContentfulPageData.ForSchema;
29
+ };
30
+ namespace GetGroup {
31
+ type Result = Omit<PageClient.GetGroup.Result, 'page'> & {
32
+ page?: FlattedPageData;
33
+ };
34
+ }
35
+ namespace Get {
36
+ type Result = FlattedPageData | undefined;
37
+ }
38
+ namespace Update {
39
+ type Result = Omit<PageClient.Update.Result, 'page'> & {
40
+ page?: FlattedPageData;
41
+ };
42
+ }
43
+ }