@datocms/cma-client 5.8.0 → 6.0.0

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.
@@ -90,7 +90,7 @@ class Client {
90
90
  return this.config.baseUrl || Client.defaultBaseUrl;
91
91
  }
92
92
  request(options) {
93
- return (0, rest_client_utils_1.request)(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v5.8.0', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: Object.assign(Object.assign(Object.assign({}, (this.config.extraHeaders || {})), (this.config.environment
93
+ return (0, rest_client_utils_1.request)(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v6.0.0', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: Object.assign(Object.assign(Object.assign({}, (this.config.extraHeaders || {})), (this.config.environment
94
94
  ? { 'X-Environment': this.config.environment }
95
95
  : {})), { 'X-API-Version': '3' }), fetchJobResult: (jobId) => {
96
96
  return this.jobResultsFetcher
@@ -1,10 +1,149 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
19
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
20
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
21
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
22
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
23
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
24
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
25
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
26
+ function fulfill(value) { resume("next", value); }
27
+ function reject(value) { resume("throw", value); }
28
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
29
+ };
2
30
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
32
  };
5
33
  Object.defineProperty(exports, "__esModule", { value: true });
6
34
  const Upload_js_1 = __importDefault(require("../generated/resources/Upload.js"));
35
+ const defaultFieldMetadata_js_1 = require("../utilities/defaultFieldMetadata.js");
36
+ /**
37
+ * `default_field_metadata` travels in one of two shapes, and which one an
38
+ * environment speaks depends on the `non_localized_focal_points` opt-in — each
39
+ * rejects the other with `422 INVALID_FORMAT`. This resource hides that: you
40
+ * always read and write the field-keyed shape the types describe, and the
41
+ * legacy one is converted to and from as needed.
42
+ *
43
+ * The raw methods are deliberately left alone: they are the escape hatch for
44
+ * anyone who needs to see what actually goes over the wire.
45
+ */
7
46
  class UploadResource extends Upload_js_1.default {
47
+ /**
48
+ * Create a new upload
49
+ *
50
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/create
51
+ *
52
+ * @throws {ApiError}
53
+ * @throws {TimeoutError}
54
+ */
55
+ create(body) {
56
+ const _super = Object.create(null, {
57
+ create: { get: () => super.create }
58
+ });
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ return (0, defaultFieldMetadata_js_1.normalizeUpload)(yield _super.create.call(this, yield (0, defaultFieldMetadata_js_1.encodeDefaultFieldMetadata)(this.client, body)));
61
+ });
62
+ }
63
+ /**
64
+ * Update an upload
65
+ *
66
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/update
67
+ *
68
+ * @throws {ApiError}
69
+ * @throws {TimeoutError}
70
+ */
71
+ update(uploadId, body, queryParams) {
72
+ const _super = Object.create(null, {
73
+ update: { get: () => super.update }
74
+ });
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ return (0, defaultFieldMetadata_js_1.normalizeUpload)(yield _super.update.call(this, uploadId, yield (0, defaultFieldMetadata_js_1.encodeDefaultFieldMetadata)(this.client, body), queryParams));
77
+ });
78
+ }
79
+ /**
80
+ * Retrieve an upload
81
+ *
82
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/self
83
+ *
84
+ * @throws {ApiError}
85
+ * @throws {TimeoutError}
86
+ */
87
+ find(uploadId) {
88
+ const _super = Object.create(null, {
89
+ find: { get: () => super.find }
90
+ });
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ return (0, defaultFieldMetadata_js_1.normalizeUpload)(yield _super.find.call(this, uploadId));
93
+ });
94
+ }
95
+ /**
96
+ * List all uploads
97
+ *
98
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/instances
99
+ *
100
+ * @throws {ApiError}
101
+ * @throws {TimeoutError}
102
+ */
103
+ list(queryParams) {
104
+ const _super = Object.create(null, {
105
+ list: { get: () => super.list }
106
+ });
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ return (yield _super.list.call(this, queryParams)).map(defaultFieldMetadata_js_1.normalizeUpload);
109
+ });
110
+ }
111
+ /**
112
+ * Async iterator to auto-paginate over elements returned by list()
113
+ *
114
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/instances
115
+ *
116
+ * @throws {ApiError}
117
+ * @throws {TimeoutError}
118
+ */
119
+ listPagedIterator(...args) {
120
+ const _super = Object.create(null, {
121
+ listPagedIterator: { get: () => super.listPagedIterator }
122
+ });
123
+ return __asyncGenerator(this, arguments, function* listPagedIterator_1() {
124
+ var _a, e_1, _b, _c;
125
+ try {
126
+ for (var _d = true, _e = __asyncValues(_super.listPagedIterator.call(this, ...args)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a;) {
127
+ _c = _f.value;
128
+ _d = false;
129
+ try {
130
+ const upload = _c;
131
+ yield yield __await((0, defaultFieldMetadata_js_1.normalizeUpload)(upload));
132
+ }
133
+ finally {
134
+ _d = true;
135
+ }
136
+ }
137
+ }
138
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
139
+ finally {
140
+ try {
141
+ if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
142
+ }
143
+ finally { if (e_1) throw e_1.error; }
144
+ }
145
+ });
146
+ }
8
147
  }
9
148
  exports.default = UploadResource;
10
149
  //# sourceMappingURL=Upload.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Upload.js","sourceRoot":"","sources":["../../../src/resources/Upload.ts"],"names":[],"mappings":";;;;;AAAA,iFAA0D;AAmC1D,MAAqB,cAAe,SAAQ,mBAAU;CAAG;AAAzD,iCAAyD"}
1
+ {"version":3,"file":"Upload.js","sourceRoot":"","sources":["../../../src/resources/Upload.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,iFAA0D;AAC1D,kFAG8C;AAE9C;;;;;;;;;GASG;AACH,MAAqB,cAAe,SAAQ,mBAAU;IACpD;;;;;;;OAOG;IACG,MAAM,CAAC,IAAiC;;;;;YAC5C,OAAO,IAAA,yCAAe,EACpB,MAAM,OAAM,MAAM,YAAC,MAAM,IAAA,oDAA0B,EAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CACxE,CAAC;QACJ,CAAC;KAAA;IAED;;;;;;;OAOG;IACG,MAAM,CACV,QAAsC,EACtC,IAAiC,EACjC,WAA6C;;;;;YAE7C,OAAO,IAAA,yCAAe,EACpB,MAAM,OAAM,MAAM,YAChB,QAAQ,EACR,MAAM,IAAA,oDAA0B,EAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,EACnD,WAAW,CACZ,CACF,CAAC;QACJ,CAAC;KAAA;IAED;;;;;;;OAOG;IACG,IAAI,CAAC,QAAsC;;;;;YAC/C,OAAO,IAAA,yCAAe,EAAC,MAAM,OAAM,IAAI,YAAC,QAAQ,CAAC,CAAC,CAAC;QACrD,CAAC;KAAA;IAED;;;;;;;OAOG;IACG,IAAI,CAAC,WAAgD;;;;;YACzD,OAAO,CAAC,MAAM,OAAM,IAAI,YAAC,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,yCAAe,CAAC,CAAC;QAC9D,CAAC;KAAA;IAED;;;;;;;OAOG;IACI,iBAAiB,CACtB,GAAG,IAAiD;;;;;;;gBAEpD,KAA2B,eAAA,KAAA,cAAA,OAAM,iBAAiB,YAAC,GAAG,IAAI,EAAC,IAAA;oBAAhC,cAAgC;oBAAhC,WAAgC;;wBAAhD,MAAM,MAAM,KAAA,CAAA;wBACrB,oBAAM,IAAA,yCAAe,EAAC,MAAM,CAAC,CAAA,CAAC;;;;;iBAC/B;;;;;;;;;QACH,CAAC;KAAA;CACF;AA5ED,iCA4EC"}
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.encodeDefaultFieldMetadata = exports.normalizeUpload = exports.toLocaleKeyed = exports.fromLocaleKeyed = exports.isFieldKeyed = void 0;
13
+ const environmentSettings_js_1 = require("./environmentSettings.js");
14
+ /**
15
+ * Tells the two wire shapes apart. A field-keyed payload always carries a
16
+ * top-level `focal_point`, and a locale-keyed one never can: its keys are
17
+ * locale codes, and no locale code is the literal string `focal_point`.
18
+ */
19
+ function isFieldKeyed(metadata) {
20
+ return 'focal_point' in metadata;
21
+ }
22
+ exports.isFieldKeyed = isFieldKeyed;
23
+ /**
24
+ * Collapses a legacy response payload into the field-keyed shape. The API
25
+ * replicates the non-localized values into every locale entry on read, so every
26
+ * entry carries the same value and the first one is enough.
27
+ */
28
+ function fromLocaleKeyed(byLocale) {
29
+ const alt = {};
30
+ const title = {};
31
+ const customData = {};
32
+ let focalPoint = null;
33
+ let posterTime = null;
34
+ let seenAnEntry = false;
35
+ for (const [locale, entry] of Object.entries(byLocale)) {
36
+ alt[locale] = entry.alt;
37
+ title[locale] = entry.title;
38
+ customData[locale] = entry.custom_data;
39
+ if (!seenAnEntry) {
40
+ seenAnEntry = true;
41
+ focalPoint = entry.focal_point;
42
+ posterTime = entry.poster_time;
43
+ }
44
+ }
45
+ return {
46
+ alt,
47
+ title,
48
+ custom_data: customData,
49
+ focal_point: focalPoint,
50
+ poster_time: posterTime,
51
+ };
52
+ }
53
+ exports.fromLocaleKeyed = fromLocaleKeyed;
54
+ /**
55
+ * Rewrites a field-keyed patch into the legacy locale-keyed one: the localized
56
+ * keys are pivoted per locale, and the non-localized ones ride along on a
57
+ * single entry. The API takes `focal_point` / `poster_time` off the first entry
58
+ * that carries one and ignores the rest, so writing them once is enough — on
59
+ * any locale the patch already touches, or the environment's first one when it
60
+ * touches none.
61
+ */
62
+ function toLocaleKeyed({ alt, title, custom_data, focal_point, poster_time, }, environmentLocales) {
63
+ var _a;
64
+ const result = {};
65
+ function entryFor(locale) {
66
+ var _a;
67
+ const entry = (_a = result[locale]) !== null && _a !== void 0 ? _a : {};
68
+ result[locale] = entry;
69
+ return entry;
70
+ }
71
+ for (const [locale, value] of Object.entries(alt !== null && alt !== void 0 ? alt : {})) {
72
+ entryFor(locale).alt = value;
73
+ }
74
+ for (const [locale, value] of Object.entries(title !== null && title !== void 0 ? title : {})) {
75
+ entryFor(locale).title = value;
76
+ }
77
+ for (const [locale, value] of Object.entries(custom_data !== null && custom_data !== void 0 ? custom_data : {})) {
78
+ entryFor(locale).custom_data = value;
79
+ }
80
+ if (focal_point !== undefined || poster_time !== undefined) {
81
+ const locale = (_a = Object.keys(result)[0]) !== null && _a !== void 0 ? _a : environmentLocales[0];
82
+ if (locale) {
83
+ if (focal_point !== undefined)
84
+ entryFor(locale).focal_point = focal_point;
85
+ if (poster_time !== undefined)
86
+ entryFor(locale).poster_time = poster_time;
87
+ }
88
+ }
89
+ return result;
90
+ }
91
+ exports.toLocaleKeyed = toLocaleKeyed;
92
+ /**
93
+ * Normalizes an upload as served by the API into the field-keyed shape the
94
+ * `Upload` type describes. Returns the entity untouched — no copy — when it is
95
+ * already field-keyed, which is the case for every environment that has taken
96
+ * the opt-in.
97
+ */
98
+ function normalizeUpload(upload) {
99
+ const metadata = upload.default_field_metadata;
100
+ if (!metadata || isFieldKeyed(metadata)) {
101
+ return upload;
102
+ }
103
+ return Object.assign(Object.assign({}, upload), { default_field_metadata: fromLocaleKeyed(metadata) });
104
+ }
105
+ exports.normalizeUpload = normalizeUpload;
106
+ /**
107
+ * Encodes a `create` / `update` body into the shape the environment accepts.
108
+ *
109
+ * The environment is only consulted when there is metadata to encode, so a
110
+ * client that never writes asset metadata never asks — and when it does ask,
111
+ * the lookup is memoized and shared, so a batch of writes pays for one.
112
+ */
113
+ function encodeDefaultFieldMetadata(client, body) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ const metadata = body.default_field_metadata;
116
+ if (!metadata) {
117
+ return body;
118
+ }
119
+ if (yield (0, environmentSettings_js_1.isEnvironmentFlagActive)(client, 'non_localized_focal_points')) {
120
+ return body;
121
+ }
122
+ const { locales } = yield (0, environmentSettings_js_1.fetchEnvironmentSettings)(client);
123
+ return Object.assign(Object.assign({}, body), {
124
+ // The declared type describes the field-keyed shape, which is what callers
125
+ // write. The legacy payload is this module's business.
126
+ default_field_metadata: toLocaleKeyed(metadata, locales) });
127
+ });
128
+ }
129
+ exports.encodeDefaultFieldMetadata = encodeDefaultFieldMetadata;
130
+ //# sourceMappingURL=defaultFieldMetadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaultFieldMetadata.js","sourceRoot":"","sources":["../../../src/utilities/defaultFieldMetadata.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,qEAGkC;AA0ClC;;;;GAIG;AACH,SAAgB,YAAY,CAC1B,QAAsE;IAEtE,OAAO,aAAa,IAAI,QAAQ,CAAC;AACnC,CAAC;AAJD,oCAIC;AAED;;;;GAIG;AACH,SAAgB,eAAe,CAC7B,QAA+C;IAE/C,MAAM,GAAG,GAAkC,EAAE,CAAC;IAC9C,MAAM,KAAK,GAAkC,EAAE,CAAC;IAChD,MAAM,UAAU,GAA6C,EAAE,CAAC;IAEhE,IAAI,UAAU,GAAoC,IAAI,CAAC;IACvD,IAAI,UAAU,GAAkB,IAAI,CAAC;IACrC,IAAI,WAAW,GAAG,KAAK,CAAC;IAExB,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QACtD,GAAG,CAAC,MAAM,CAA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,iDAAwB,EAAC,MAAM,CAAC,CAAC;QAE3D,uCACK,IAAI;YACP,2EAA2E;YAC3E,uDAAuD;YACvD,sBAAsB,EAAE,aAAa,CACnC,QAAQ,EACR,OAAO,CACoC,IAC7C;IACJ,CAAC;CAAA;AAxBD,gEAwBC"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.isEnvironmentFlagActive = exports.fetchEnvironmentSettings = void 0;
13
+ /** How long a fetched `site` is reused before we ask again. */
14
+ const TTL_MS = 20 * 60 * 1000;
15
+ /**
16
+ * Keyed by client, so the cache covers every resource that asks and dies with
17
+ * the client that owns it.
18
+ */
19
+ const caches = new WeakMap();
20
+ /**
21
+ * `site.find()`, memoized per client for {@link TTL_MS}.
22
+ *
23
+ * The promise is cached rather than its result, so concurrent callers share one
24
+ * request instead of firing one each — the difference between one lookup and
25
+ * ten thousand when a batch job starts. A failed lookup is not cached.
26
+ *
27
+ * Everything expires on the same clock, including flags that in practice never
28
+ * change back: re-reading `site` every twenty minutes costs nothing next to the
29
+ * work these clients are doing, and it means a client stays correct across an
30
+ * activation that happens while it runs.
31
+ */
32
+ function fetchEnvironmentSettings(client) {
33
+ const cached = caches.get(client);
34
+ const now = Date.now();
35
+ if (cached && cached.expiresAt > now) {
36
+ return cached.promise;
37
+ }
38
+ const promise = client.site.find();
39
+ // A failed lookup must not be cached, or every later caller inherits it
40
+ promise.catch(() => {
41
+ var _a;
42
+ if (((_a = caches.get(client)) === null || _a === void 0 ? void 0 : _a.promise) === promise) {
43
+ caches.delete(client);
44
+ }
45
+ });
46
+ caches.set(client, { promise, expiresAt: now + TTL_MS });
47
+ return promise;
48
+ }
49
+ exports.fetchEnvironmentSettings = fetchEnvironmentSettings;
50
+ /**
51
+ * Whether a `site.meta` flag is on for the environment this client talks to.
52
+ *
53
+ * An API predating a flag omits it from the meta entirely, which the types
54
+ * don't admit but runtime does — so anything short of an explicit `true` reads
55
+ * as off.
56
+ */
57
+ function isEnvironmentFlagActive(client, flag) {
58
+ var _a;
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const site = yield fetchEnvironmentSettings(client);
61
+ return ((_a = site.meta) === null || _a === void 0 ? void 0 : _a[flag]) === true;
62
+ });
63
+ }
64
+ exports.isEnvironmentFlagActive = isEnvironmentFlagActive;
65
+ //# sourceMappingURL=environmentSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environmentSettings.js","sourceRoot":"","sources":["../../../src/utilities/environmentSettings.ts"],"names":[],"mappings":";;;;;;;;;;;;AAkBA,+DAA+D;AAC/D,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAI9B;;;GAGG;AACH,MAAM,MAAM,GAAG,IAAI,OAAO,EAAiB,CAAC;AAE5C;;;;;;;;;;;GAWG;AACH,SAAgB,wBAAwB,CACtC,MAAc;IAEd,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAClC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAEvB,IAAI,MAAM,IAAI,MAAM,CAAC,SAAS,GAAG,GAAG,EAAE;QACpC,OAAO,MAAM,CAAC,OAAO,CAAC;KACvB;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IAEnC,wEAAwE;IACxE,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE;;QACjB,IAAI,CAAA,MAAA,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,0CAAE,OAAO,MAAK,OAAO,EAAE;YAC3C,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;SACvB;IACH,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,GAAG,MAAM,EAAE,CAAC,CAAC;IAEzD,OAAO,OAAO,CAAC;AACjB,CAAC;AAtBD,4DAsBC;AAED;;;;;;GAMG;AACH,SAAsB,uBAAuB,CAC3C,MAAc,EACd,IAAqB;;;QAErB,MAAM,IAAI,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;QAEpD,OAAO,CAAA,MAAC,IAAI,CAAC,IAA+C,0CAAG,IAAI,CAAC,MAAK,IAAI,CAAC;;CAC/E;AAPD,0DAOC"}
@@ -64,7 +64,7 @@ export class Client {
64
64
  return this.config.baseUrl || Client.defaultBaseUrl;
65
65
  }
66
66
  request(options) {
67
- return request(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v5.8.0', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: Object.assign(Object.assign(Object.assign({}, (this.config.extraHeaders || {})), (this.config.environment
67
+ return request(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v6.0.0', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: Object.assign(Object.assign(Object.assign({}, (this.config.extraHeaders || {})), (this.config.environment
68
68
  ? { 'X-Environment': this.config.environment }
69
69
  : {})), { 'X-API-Version': '3' }), fetchJobResult: (jobId) => {
70
70
  return this.jobResultsFetcher
@@ -4,7 +4,7 @@ export * from './fieldTypes/index.js';
4
4
  export { Client } from './generated/Client.js';
5
5
  export type { ClientConfigOptions } from './generated/Client.js';
6
6
  export * as Resources from './generated/resources/index.js';
7
- export type { UploadLocaleKeyedDefaultFieldMetadata, UploadLocaleKeyedDefaultFieldMetadataInRequest, } from './resources/Upload.js';
7
+ export type { UploadLocaleKeyedDefaultFieldMetadata, UploadLocaleKeyedDefaultFieldMetadataInRequest, } from './utilities/defaultFieldMetadata.js';
8
8
  export * from './utilities/buildBlockRecord.js';
9
9
  export * from './utilities/duplicateBlockRecord.js';
10
10
  export * from './utilities/fieldsContainingReferences.js';
@@ -1,44 +1,59 @@
1
+ import type * as ApiTypes from '../generated/ApiTypes.js';
1
2
  import BaseUpload from '../generated/resources/Upload.js';
2
3
  /**
3
- * Legacy locale-keyed shape of an upload's `default_field_metadata`, as
4
- * returned and accepted by environments where the `non_localized_focal_points`
5
- * opt-in is inactive.
4
+ * `default_field_metadata` travels in one of two shapes, and which one an
5
+ * environment speaks depends on the `non_localized_focal_points` opt-in — each
6
+ * rejects the other with `422 INVALID_FORMAT`. This resource hides that: you
7
+ * always read and write the field-keyed shape the types describe, and the
8
+ * legacy one is converted to and from as needed.
6
9
  *
7
- * The generated `Upload.default_field_metadata` type reflects the new
8
- * field-keyed shape (the path forward). This type is provided so consumers
9
- * targeting opted-out environments during the transition can cast the
10
- * response value to a structurally accurate shape.
10
+ * The raw methods are deliberately left alone: they are the escape hatch for
11
+ * anyone who needs to see what actually goes over the wire.
11
12
  */
12
- export type UploadLocaleKeyedDefaultFieldMetadata = {
13
- [localeCode: string]: {
14
- alt: string | null;
15
- title: string | null;
16
- custom_data: {
17
- [k: string]: unknown;
18
- };
19
- focal_point: {
20
- x: number;
21
- y: number;
22
- } | null;
23
- };
24
- };
25
- /**
26
- * Legacy locale-keyed shape accepted on `create` / `update` request bodies
27
- * by environments where the `non_localized_focal_points` opt-in is inactive.
28
- * All fields are optional, matching the partial-write contract.
29
- */
30
- export type UploadLocaleKeyedDefaultFieldMetadataInRequest = {
31
- [localeCode: string]: {
32
- alt?: string | null;
33
- title?: string | null;
34
- custom_data?: {
35
- [k: string]: unknown;
36
- };
37
- focal_point?: {
38
- x: number;
39
- y: number;
40
- } | null;
41
- };
42
- };
43
13
  export default class UploadResource extends BaseUpload {
14
+ /**
15
+ * Create a new upload
16
+ *
17
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/create
18
+ *
19
+ * @throws {ApiError}
20
+ * @throws {TimeoutError}
21
+ */
22
+ create(body: ApiTypes.UploadCreateSchema): Promise<ApiTypes.Upload>;
23
+ /**
24
+ * Update an upload
25
+ *
26
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/update
27
+ *
28
+ * @throws {ApiError}
29
+ * @throws {TimeoutError}
30
+ */
31
+ update(uploadId: string | ApiTypes.UploadData, body: ApiTypes.UploadUpdateSchema, queryParams?: ApiTypes.UploadUpdateHrefSchema): Promise<ApiTypes.Upload>;
32
+ /**
33
+ * Retrieve an upload
34
+ *
35
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/self
36
+ *
37
+ * @throws {ApiError}
38
+ * @throws {TimeoutError}
39
+ */
40
+ find(uploadId: string | ApiTypes.UploadData): Promise<ApiTypes.Upload>;
41
+ /**
42
+ * List all uploads
43
+ *
44
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/instances
45
+ *
46
+ * @throws {ApiError}
47
+ * @throws {TimeoutError}
48
+ */
49
+ list(queryParams?: ApiTypes.UploadInstancesHrefSchema): Promise<ApiTypes.Upload[]>;
50
+ /**
51
+ * Async iterator to auto-paginate over elements returned by list()
52
+ *
53
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/instances
54
+ *
55
+ * @throws {ApiError}
56
+ * @throws {TimeoutError}
57
+ */
58
+ listPagedIterator(...args: Parameters<BaseUpload['listPagedIterator']>): AsyncGenerator<ApiTypes.Upload, void, unknown>;
44
59
  }
@@ -1,4 +1,143 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
11
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
12
+ var m = o[Symbol.asyncIterator], i;
13
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
14
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
15
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
16
+ };
17
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
18
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
19
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
20
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
21
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
22
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
23
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
24
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
25
+ function fulfill(value) { resume("next", value); }
26
+ function reject(value) { resume("throw", value); }
27
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
28
+ };
1
29
  import BaseUpload from '../generated/resources/Upload.js';
30
+ import { encodeDefaultFieldMetadata, normalizeUpload, } from '../utilities/defaultFieldMetadata.js';
31
+ /**
32
+ * `default_field_metadata` travels in one of two shapes, and which one an
33
+ * environment speaks depends on the `non_localized_focal_points` opt-in — each
34
+ * rejects the other with `422 INVALID_FORMAT`. This resource hides that: you
35
+ * always read and write the field-keyed shape the types describe, and the
36
+ * legacy one is converted to and from as needed.
37
+ *
38
+ * The raw methods are deliberately left alone: they are the escape hatch for
39
+ * anyone who needs to see what actually goes over the wire.
40
+ */
2
41
  export default class UploadResource extends BaseUpload {
42
+ /**
43
+ * Create a new upload
44
+ *
45
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/create
46
+ *
47
+ * @throws {ApiError}
48
+ * @throws {TimeoutError}
49
+ */
50
+ create(body) {
51
+ const _super = Object.create(null, {
52
+ create: { get: () => super.create }
53
+ });
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ return normalizeUpload(yield _super.create.call(this, yield encodeDefaultFieldMetadata(this.client, body)));
56
+ });
57
+ }
58
+ /**
59
+ * Update an upload
60
+ *
61
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/update
62
+ *
63
+ * @throws {ApiError}
64
+ * @throws {TimeoutError}
65
+ */
66
+ update(uploadId, body, queryParams) {
67
+ const _super = Object.create(null, {
68
+ update: { get: () => super.update }
69
+ });
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ return normalizeUpload(yield _super.update.call(this, uploadId, yield encodeDefaultFieldMetadata(this.client, body), queryParams));
72
+ });
73
+ }
74
+ /**
75
+ * Retrieve an upload
76
+ *
77
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/self
78
+ *
79
+ * @throws {ApiError}
80
+ * @throws {TimeoutError}
81
+ */
82
+ find(uploadId) {
83
+ const _super = Object.create(null, {
84
+ find: { get: () => super.find }
85
+ });
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ return normalizeUpload(yield _super.find.call(this, uploadId));
88
+ });
89
+ }
90
+ /**
91
+ * List all uploads
92
+ *
93
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/instances
94
+ *
95
+ * @throws {ApiError}
96
+ * @throws {TimeoutError}
97
+ */
98
+ list(queryParams) {
99
+ const _super = Object.create(null, {
100
+ list: { get: () => super.list }
101
+ });
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ return (yield _super.list.call(this, queryParams)).map(normalizeUpload);
104
+ });
105
+ }
106
+ /**
107
+ * Async iterator to auto-paginate over elements returned by list()
108
+ *
109
+ * Read more: https://www.datocms.com/docs/content-management-api/resources/upload/instances
110
+ *
111
+ * @throws {ApiError}
112
+ * @throws {TimeoutError}
113
+ */
114
+ listPagedIterator(...args) {
115
+ const _super = Object.create(null, {
116
+ listPagedIterator: { get: () => super.listPagedIterator }
117
+ });
118
+ return __asyncGenerator(this, arguments, function* listPagedIterator_1() {
119
+ var _a, e_1, _b, _c;
120
+ try {
121
+ for (var _d = true, _e = __asyncValues(_super.listPagedIterator.call(this, ...args)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a;) {
122
+ _c = _f.value;
123
+ _d = false;
124
+ try {
125
+ const upload = _c;
126
+ yield yield __await(normalizeUpload(upload));
127
+ }
128
+ finally {
129
+ _d = true;
130
+ }
131
+ }
132
+ }
133
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
134
+ finally {
135
+ try {
136
+ if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
137
+ }
138
+ finally { if (e_1) throw e_1.error; }
139
+ }
140
+ });
141
+ }
3
142
  }
4
143
  //# sourceMappingURL=Upload.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Upload.js","sourceRoot":"","sources":["../../../src/resources/Upload.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,kCAAkC,CAAC;AAmC1D,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,UAAU;CAAG"}
1
+ {"version":3,"file":"Upload.js","sourceRoot":"","sources":["../../../src/res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