From e491f2a085399b652cd30e30a6ea1d4a7acc666a Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Mon, 16 Jul 2018 23:08:16 -0400 Subject: [PATCH] move errors to utils lib --- .../netlify-cms-core/src/actions/editorialWorkflow.js | 2 +- .../netlify-cms-core/src/backends/git-gateway/API.js | 2 +- .../src/backends/git-gateway/GitHubAPI.js | 2 +- packages/netlify-cms-core/src/backends/github/API.js | 6 +++--- packages/netlify-cms-core/src/backends/gitlab/API.js | 11 ++++++----- .../src/backends/test-repo/implementation.js | 2 +- .../netlify-cms-core/src/valueObjects/errors/index.js | 3 --- .../errors => netlify-cms-lib-util/src}/APIError.js | 0 .../src}/EditorialWorkflowError.js | 0 packages/netlify-cms-lib-util/src/index.js | 4 ++++ 10 files changed, 17 insertions(+), 15 deletions(-) delete mode 100644 packages/netlify-cms-core/src/valueObjects/errors/index.js rename packages/{netlify-cms-core/src/valueObjects/errors => netlify-cms-lib-util/src}/APIError.js (100%) rename packages/{netlify-cms-core/src/valueObjects/errors => netlify-cms-lib-util/src}/EditorialWorkflowError.js (100%) diff --git a/packages/netlify-cms-core/src/actions/editorialWorkflow.js b/packages/netlify-cms-core/src/actions/editorialWorkflow.js index 029e42f2..7570a602 100644 --- a/packages/netlify-cms-core/src/actions/editorialWorkflow.js +++ b/packages/netlify-cms-core/src/actions/editorialWorkflow.js @@ -6,7 +6,7 @@ import { currentBackend } from 'Backends/backend'; import { getAsset } from 'Reducers'; import { selectFields } from 'Reducers/collections'; import { status, EDITORIAL_WORKFLOW } from 'Constants/publishModes'; -import { EditorialWorkflowError } from "ValueObjects/errors"; +import EditorialWorkflowError from 'netlify-cms-lib-util/EditorialWorkflowError'; import { loadEntry } from './entries'; import ValidationErrorTypes from 'Constants/validationErrorTypes'; diff --git a/packages/netlify-cms-core/src/backends/git-gateway/API.js b/packages/netlify-cms-core/src/backends/git-gateway/API.js index 332c7ced..88aa7207 100644 --- a/packages/netlify-cms-core/src/backends/git-gateway/API.js +++ b/packages/netlify-cms-core/src/backends/git-gateway/API.js @@ -1,5 +1,5 @@ import GithubAPI from "Backends/github/API"; -import { APIError } from "ValueObjects/errors"; +import APIError from "netlify-cms-lib-util/APIError"; export default class API extends GithubAPI { constructor(config) { diff --git a/packages/netlify-cms-core/src/backends/git-gateway/GitHubAPI.js b/packages/netlify-cms-core/src/backends/git-gateway/GitHubAPI.js index 332c7ced..88aa7207 100644 --- a/packages/netlify-cms-core/src/backends/git-gateway/GitHubAPI.js +++ b/packages/netlify-cms-core/src/backends/git-gateway/GitHubAPI.js @@ -1,5 +1,5 @@ import GithubAPI from "Backends/github/API"; -import { APIError } from "ValueObjects/errors"; +import APIError from "netlify-cms-lib-util/APIError"; export default class API extends GithubAPI { constructor(config) { diff --git a/packages/netlify-cms-core/src/backends/github/API.js b/packages/netlify-cms-core/src/backends/github/API.js index 440f83ff..c58ac576 100644 --- a/packages/netlify-cms-core/src/backends/github/API.js +++ b/packages/netlify-cms-core/src/backends/github/API.js @@ -1,10 +1,10 @@ import localForage from "netlify-cms-lib-util/localForage"; import { Base64 } from "js-base64"; -import { uniq, initial, last, get, find, hasIn } from "lodash"; +import { uniq, initial, last, get, find, hasIn, partial } from "lodash"; import { filterPromises, resolvePromiseProperties } from "netlify-cms-lib-util/promise"; import AssetProxy from "ValueObjects/AssetProxy"; import { SIMPLE, EDITORIAL_WORKFLOW, status } from "Constants/publishModes"; -import { APIError, EditorialWorkflowError } from "ValueObjects/errors"; +import { APIError, EditorialWorkflowError } from "netlify-cms-lib-util"; const CMS_BRANCH_PREFIX = 'cms/'; @@ -713,7 +713,7 @@ export default class API { } uploadBlob(item) { - const content = item instanceof AssetProxy ? item.toBase64() : this.toBase64(item.raw); + const content = get(item, 'toBase64', partial(this.toBase64, item.raw))(); return content.then(contentBase64 => this.request(`${ this.repoURL }/git/blobs`, { method: "POST", diff --git a/packages/netlify-cms-core/src/backends/gitlab/API.js b/packages/netlify-cms-core/src/backends/gitlab/API.js index 1c63ac72..b4cc58a2 100644 --- a/packages/netlify-cms-core/src/backends/gitlab/API.js +++ b/packages/netlify-cms-core/src/backends/gitlab/API.js @@ -1,11 +1,11 @@ import localForage from "netlify-cms-lib-util/localForage"; import { Base64 } from "js-base64"; import { fromJS, List, Map } from "immutable"; -import { cond, flow, isString, partial, partialRight, pick, omit, set, update } from "lodash"; +import { cond, flow, isString, partial, partialRight, pick, omit, set, update, get } from "lodash"; import unsentRequest from "netlify-cms-lib-util/unsentRequest"; import { then } from "netlify-cms-lib-util/promise"; +import APIError from "netlify-cms-lib-util/APIError"; import AssetProxy from "ValueObjects/AssetProxy"; -import { APIError } from "ValueObjects/errors"; import Cursor from "ValueObjects/Cursor" export default class API { @@ -17,8 +17,9 @@ export default class API { this.repoURL = `/projects/${ encodeURIComponent(this.repo) }`; } - withAuthorizationHeaders = req => - unsentRequest.withHeaders(this.token ? { Authorization: `Bearer ${ this.token }` } : {}, req); + withAuthorizationHeaders = req => ( + unsentRequest.withHeaders(this.token ? { Authorization: `Bearer ${ this.token }` } : {}, req) + ); buildRequest = req => flow([ unsentRequest.withRoot(this.api_root), @@ -189,7 +190,7 @@ export default class API { toBase64 = str => Promise.resolve(Base64.encode(str)); fromBase64 = str => Base64.decode(str); uploadAndCommit = async (item, { commitMessage, updateFile = false, branch = this.branch, author = this.commitAuthor }) => { - const content = await (item instanceof AssetProxy ? item.toBase64() : this.toBase64(item.raw)); + const content = get(item, 'toBase64', partial(this.toBase64, item.raw))(); const file_path = item.path.replace(/^\//, ""); const action = (updateFile ? "update" : "create"); const encoding = "base64"; diff --git a/packages/netlify-cms-core/src/backends/test-repo/implementation.js b/packages/netlify-cms-core/src/backends/test-repo/implementation.js index 319426e2..44b1f127 100644 --- a/packages/netlify-cms-core/src/backends/test-repo/implementation.js +++ b/packages/netlify-cms-core/src/backends/test-repo/implementation.js @@ -2,7 +2,7 @@ import { remove, attempt, isError, take } from 'lodash'; import uuid from 'uuid/v4'; import { fromJS } from 'immutable'; import { EDITORIAL_WORKFLOW, status } from 'Constants/publishModes'; -import { EditorialWorkflowError } from 'ValueObjects/errors'; +import EditorialWorkflowError from 'netlify-cms-lib-util/EditorialWorkflowError'; import Cursor, { CURSOR_COMPATIBILITY_SYMBOL } from 'ValueObjects/Cursor' import AuthenticationPage from './AuthenticationPage'; diff --git a/packages/netlify-cms-core/src/valueObjects/errors/index.js b/packages/netlify-cms-core/src/valueObjects/errors/index.js deleted file mode 100644 index 113aad8a..00000000 --- a/packages/netlify-cms-core/src/valueObjects/errors/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export { default as APIError } from './APIError'; -export { default as EditorialWorkflowError } from './EditorialWorkflowError'; - diff --git a/packages/netlify-cms-core/src/valueObjects/errors/APIError.js b/packages/netlify-cms-lib-util/src/APIError.js similarity index 100% rename from packages/netlify-cms-core/src/valueObjects/errors/APIError.js rename to packages/netlify-cms-lib-util/src/APIError.js diff --git a/packages/netlify-cms-core/src/valueObjects/errors/EditorialWorkflowError.js b/packages/netlify-cms-lib-util/src/EditorialWorkflowError.js similarity index 100% rename from packages/netlify-cms-core/src/valueObjects/errors/EditorialWorkflowError.js rename to packages/netlify-cms-lib-util/src/EditorialWorkflowError.js diff --git a/packages/netlify-cms-lib-util/src/index.js b/packages/netlify-cms-lib-util/src/index.js index 432f59e0..edab24d8 100644 --- a/packages/netlify-cms-lib-util/src/index.js +++ b/packages/netlify-cms-lib-util/src/index.js @@ -2,6 +2,8 @@ import localForage from './localForage'; import { resolvePath, basename, fileExtensionWithSeparator, fileExtension } from './path'; import { filterPromises, resolvePromiseProperties, then } from './promise'; import unsentRequest from './unsentRequest'; +import APIError from './APIError'; +import EditorialWorkflowError from './EditorialWorkflowError'; export { localForage, @@ -13,4 +15,6 @@ export { resolvePromiseProperties, then, unsentRequest, + APIError, + EditorialWorkflowError, };