Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Support for CMA v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Irene authored and stefanoverna committed Oct 5, 2019
1 parent 6632d8a commit 8e28824
Show file tree
Hide file tree
Showing 53 changed files with 143,062 additions and 761 deletions.
6,840 changes: 6,840 additions & 0 deletions cassettes/account-api-account-find-update.json

Large diffs are not rendered by default.

6,895 changes: 6,895 additions & 0 deletions cassettes/account-api-site-find-all-create-update-destroy.json

Large diffs are not rendered by default.

8,216 changes: 8,216 additions & 0 deletions cassettes/cli-tool-dump.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-deploy-event-find-all-before.json

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions cassettes/site-api-deploy-event-find-all.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-deployment-environment-create-trigger-before.json

Large diffs are not rendered by default.

180 changes: 180 additions & 0 deletions cassettes/site-api-deployment-environment-create-trigger.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-field-create-find-all-update-destroy-before.json

Large diffs are not rendered by default.

421 changes: 421 additions & 0 deletions cassettes/site-api-field-create-find-all-update-destroy.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-item-batch-publishunpublish-works-before.json

Large diffs are not rendered by default.

572 changes: 572 additions & 0 deletions cassettes/site-api-item-batch-publishunpublish-works.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-item-batchdestroy-works-before.json

Large diffs are not rendered by default.

427 changes: 427 additions & 0 deletions cassettes/site-api-item-batchdestroy-works.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-item-create-find-all-update-destroy-before.json

Large diffs are not rendered by default.

772 changes: 772 additions & 0 deletions cassettes/site-api-item-create-find-all-update-destroy.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-item-creation-accepts-uncamelized-keys-before.json

Large diffs are not rendered by default.

394 changes: 394 additions & 0 deletions cassettes/site-api-item-creation-accepts-uncamelized-keys.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-item-type-create-find-all-update-destroy-before.json

Large diffs are not rendered by default.

563 changes: 563 additions & 0 deletions cassettes/site-api-item-type-create-find-all-update-destroy.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-menu-item-create-find-all-update-destroy-before.json

Large diffs are not rendered by default.

409 changes: 409 additions & 0 deletions cassettes/site-api-menu-item-create-find-all-update-destroy.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-plugins-create-find-all-update-destroy-before.json

Large diffs are not rendered by default.

246 changes: 246 additions & 0 deletions cassettes/site-api-plugins-create-find-all-update-destroy.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-site-find-update-before.json

Large diffs are not rendered by default.

189 changes: 189 additions & 0 deletions cassettes/site-api-site-find-update.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-site-invitations-create-find-all-destroy-before.json

Large diffs are not rendered by default.

346 changes: 346 additions & 0 deletions cassettes/site-api-site-invitations-create-find-all-destroy.json

Large diffs are not rendered by default.

6,740 changes: 6,740 additions & 0 deletions cassettes/site-api-upload-request-create-before.json

Large diffs are not rendered by default.

134 changes: 134 additions & 0 deletions cassettes/site-api-upload-request-create.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"scripts": {
"lint:autocorrect": "eslint ./ --fix",
"test": "eslint ./ && mocha ./test/**/*_test.js --require \"@babel/register\" --require babel-polyfill --require \"./test/support/common\" --timeout 15000",
"test": "eslint ./ && npm run mocha",
"mocha": "mocha ./test/**/*_test.js --require \"@babel/register\" --require babel-polyfill --require \"./test/support/common\" --timeout 15000",
"prepublish": "rimraf lib dist && mkdir lib dist && node build/buildApiMethods.js && node build/transformPackageJson.js && npm run lib && npm run dist",
"dist": "NODE_ENV=production webpack --progress",
"dev": "NODE_ENV=production webpack --progress --watch",
Expand Down Expand Up @@ -72,8 +73,8 @@
"parser-front-matter": "^1.6.4",
"toml": "^2.3.3",
"toml-parser": "^0.0.7",
"webpack": "^4.16.5",
"webpack-bundle-analyzer": "^2.13.1",
"webpack": "^4.35.2",
"webpack-bundle-analyzer": "^3.3.2",
"webpack-cli": "^3.1.0"
},
"dependencies": {
Expand Down Expand Up @@ -103,7 +104,8 @@
"pluralize": "^7.0.0",
"pretty-error": "^2.1.1",
"proxy-polyfill": "^0.3.0",
"pusher-js": "^4.3.0",
"pusher-js": "^4.4.0",
"qs": "^6.9.0",
"request": "^2.88.0",
"rimraf": "^2.6.2",
"speakingurl": "^14.0.1",
Expand Down
13 changes: 9 additions & 4 deletions src/Client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import queryString from 'querystring';
import qs from 'qs';
import ApiException from './ApiException';
import pkg from '../package.json';
import fetch from './utils/fetch';
import wait from './utils/wait';

const undefinedToNull = (k, v) => (v === undefined ? null : v);

function queryString(query) {
return qs.stringify(query, { arrayFormat: 'brackets' });
}

export default class Client {
constructor(token, extraHeaders, baseUrl) {
this.baseUrl = baseUrl;
Expand Down Expand Up @@ -64,13 +68,13 @@ export default class Client {
accept: 'application/json',
authorization: `Bearer ${this.token}`,
'user-agent': `js-client v${pkg.version}`,
'X-Api-Version': '2',
'X-Api-Version': '3',
};
}

buildUrl(path, params = {}) {
const query = Object.keys(params).length
? `?${queryString.stringify(params)}`
? `?${queryString(params)}`
: '';
return `${this.baseUrl}${path}${query}`;
}
Expand All @@ -89,7 +93,7 @@ export default class Client {
{ headers: fullHeaders },
);

// TODO console.log(url, fullOptions);
// console.log('---->', url, fullOptions);

return fetch(url, fullOptions)
.then((res) => {
Expand All @@ -104,6 +108,7 @@ export default class Client {
return (res.status !== 204 ? res.json() : Promise.resolve(null))
.then((body) => {
if (res.status >= 200 && res.status < 300) {
// console.log('<----', JSON.stringify(body));
return Promise.resolve(body);
}
return Promise.reject(new ApiException(res, body));
Expand Down
5 changes: 5 additions & 0 deletions src/account/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ module.exports = {
},
"subscription": {
"create": true,
"simulate": true,
"validate": true
},
"jobResult": {
"find": true
},
"siteTransfers": {
"all": true,
"find": true,
"create": true,
"destroy": true,
"simulateAccept": true,
"accept": true,
"decline": true
},
Expand Down
34 changes: 6 additions & 28 deletions src/local/Loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import Pusher from 'pusher-js/node';
import EntitiesRepo from './EntitiesRepo';

const PUSHER_API_KEY = '75e6ef0fe5d39f481626';

export default class Loader {
constructor(client, previewMode = false) {
this.client = client;
Expand All @@ -18,37 +15,18 @@ export default class Loader {
{ deserializeResponse: false, allPages: true },
),
this.client.uploads.all({}, { deserializeResponse: false, allPages: true }),
])
.then(([site, items, uploads]) => {
this.siteId = site.data.id;
this.entitiesRepo.upsertEntities(site, items, uploads);
});
]).then(([site, items, uploads]) => {
this.siteId = site.data.id;
this.entitiesRepo.upsertEntities(site, items, uploads);
});
}

async watch(notifier) {
if (!this.siteId) {
await this.load();
}

const pusher = new Pusher(
PUSHER_API_KEY,
{
authEndpoint: 'https://site-api.datocms.com/pusher/authenticate',
auth: {
headers: {
Authorization: `Bearer ${this.client.rawClient.token}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
},
},
);

const watcher = pusher.subscribe(`private-site-${this.siteId}`);

watcher.bind('pusher:subscription_error', () => {
process.stdout.write('Could not subscribe to the project live events... :(');
});
const [watcher, disconnect] = await this.client.subscribeToChannel(this.siteId);

const addEventListener = (eventName, entitiesRepoRefresher) => {
watcher.bind(eventName, (data) => {
Expand Down Expand Up @@ -120,6 +98,6 @@ export default class Loader {
});
});

return pusher.disconnect.bind(pusher);
return disconnect;
}
}
29 changes: 26 additions & 3 deletions src/local/fields/File.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import buildFileUrl from '../../utils/buildFileUrl';
import i18n from '../../utils/i18n';

export default class File {
constructor(value, {
itemsRepo,
imgixHost,
}) {
this.value = value;
this.imgixHost = imgixHost;
this.itemsRepo = itemsRepo;
this.upload = itemsRepo.entitiesRepo.findEntity('upload', value);
this.upload = itemsRepo.entitiesRepo.findEntity('upload', value.uploadId);
}

get path() {
Expand All @@ -30,12 +32,33 @@ export default class File {
return this.upload.height;
}

get author() {
return this.upload.author;
}

get notes() {
return this.upload.notes;
}

get copyright() {
return this.upload.copyright;
}

get alt() {
return this.upload.alt;
return this.value.alt
|| this.upload.defaultFieldMetadata[i18n.locale].alt;
}

get title() {
return this.upload.title;
return this.value.title
|| this.upload.defaultFieldMetadata[i18n.locale].title;
}

get customData() {
return Object.assign(
this.value.customData,
this.upload.defaultFieldMetadata[i18n.locale].customData,
);
}

url(params = {}) {
Expand Down
14 changes: 6 additions & 8 deletions src/site/SiteClient.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import uploadFile from '../upload/uploadFile';
import generateClient from '../utils/generateClient';
import cache from './cache';
import subscribeToChannel from '../utils/subscribeToChannel';

export default generateClient(
'site-api',
cache,
{
uploadFile,
uploadImage: uploadFile,
},
);
export default generateClient('site-api', cache, {
uploadFile,
uploadImage: uploadFile,
subscribeToChannel,
});
17 changes: 17 additions & 0 deletions src/site/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = {
"find": true,
"destroy": true
},
"jobResult": {
"find": true
},
"deployEvents": {
"all": true,
"find": true
Expand Down Expand Up @@ -132,5 +135,19 @@ module.exports = {
"all": true,
"update": true,
"destroy": true
},
"ssoUsers": {
"all": true,
"syncSsoUsers": true,
"destroy": true
},
"ssoGroups": {
"all": true,
"update": true,
"destroy": true
},
"ssoSetting": {
"find": true,
"update": true
}
};
7 changes: 6 additions & 1 deletion src/upload/uploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default function uploadFile(client, source) {
return client.uploads.create(attributes);
})
.then((upload) => {
return Promise.resolve(upload.id);
return Promise.resolve({
uploadId: upload.id,
alt: null,
title: null,
customData: {},
});
});
}
31 changes: 10 additions & 21 deletions src/utils/deserializeJsonApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,19 @@ const findKey = (jsonApiKey, schema) => {
const findAttributes = findKey.bind(null, 'attributes');
const findMeta = findKey.bind(null, 'meta');

function deserialize(
type,
relationshipsMeta,
schema,
{
id,
attributes,
meta,
relationships,
},
) {
function deserialize(type, relationshipsMeta, schema, {
id, attributes, meta, relationships,
}) {
const result = { id };

const attrs = type === 'item'
const attrs = type === 'item' && attributes
? Object.keys(attributes).map(key => ({ key, details: null }))
: findAttributes(schema);

attrs.forEach(({ key, details }) => {
if (hasKey(attributes, key)) {
result[camelize(key)] = details && details.keepOriginalCaseOnKeys
? attributes[key]
: camelizeKeys(attributes[key]);
? attributes[key] : camelizeKeys(attributes[key]);
}
});

Expand All @@ -47,8 +38,7 @@ function deserialize(
findMeta(schema).forEach(({ key, details }) => {
if (hasKey(meta, key)) {
result.meta[camelize(key)] = details && details.keepOriginalCaseOnKeys
? meta[key]
: camelizeKeys(meta[key]);
? meta[key] : camelizeKeys(meta[key]);
}
});
}
Expand Down Expand Up @@ -78,18 +68,17 @@ function deserialize(
return result;
}

export default function deserializeJsonApi(type, link, json) {
export default function deserializeJsonApi(type, targetSchema, json) {
if (!json) {
return json;
}

const relationshipsMeta = jsonSchemaRelationships(link.targetSchema);
const relationshipsMeta = jsonSchemaRelationships(targetSchema);

const { data } = json;

if (Array.isArray(data)) {
return data.map(item => deserialize(type, relationshipsMeta, link.targetSchema, item));
return data.map(item => deserialize(type, relationshipsMeta, targetSchema, item));
}

return deserialize(type, relationshipsMeta, link.targetSchema, data);
return deserialize(type, relationshipsMeta, targetSchema, data);
}
24 changes: 18 additions & 6 deletions src/utils/findInfoForProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ export default function findInfoForProperty(propertyName, schema) {
return null;
}

let property;

if (schema.properties.data.type === 'array') {
property = schema.properties.data.items.properties[propertyName];
} else {
property = schema.properties.data.properties[propertyName];
if (schema.properties.data.items) {
return schema.properties.data.items.properties[propertyName];
}

return null;
}

if (schema.properties.data.type === 'object') {
return schema.properties.data.properties[propertyName];
}

if (schema.properties.data.anyOf) {
const subSchema = schema.properties.data.anyOf.find(x => (
x.definitions.type.example !== 'job'
));

return subSchema.properties[propertyName];
}

return property;
return null;
}
Loading

0 comments on commit 8e28824

Please sign in to comment.