Skip to content

Commit

Permalink
fix: move common api functions to a separate file (#3511)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Apr 1, 2020
1 parent 816bef5 commit 49098de
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 91 deletions.
25 changes: 0 additions & 25 deletions packages/netlify-cms-lib-util/src/API.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
import { asyncLock, AsyncLock } from './asyncLock';
import unsentRequest from './unsentRequest';

export const CMS_BRANCH_PREFIX = 'cms';
export const DEFAULT_PR_BODY = 'Automatically generated by Netlify CMS';
export const MERGE_COMMIT_MESSAGE = 'Automatically generated. Merged on Netlify CMS.';

const NETLIFY_CMS_LABEL_PREFIX = 'netlify-cms/';
export const isCMSLabel = (label: string) => label.startsWith(NETLIFY_CMS_LABEL_PREFIX);
export const labelToStatus = (label: string) => label.substr(NETLIFY_CMS_LABEL_PREFIX.length);
export const statusToLabel = (status: string) => `${NETLIFY_CMS_LABEL_PREFIX}${status}`;

export const generateContentKey = (collectionName: string, slug: string) =>
`${collectionName}/${slug}`;

export const parseContentKey = (contentKey: string) => {
const index = contentKey.indexOf('/');
return { collection: contentKey.substr(0, index), slug: contentKey.substr(index + 1) };
};

export const contentKeyFromBranch = (branch: string) => {
return branch.substring(`${CMS_BRANCH_PREFIX}/`.length);
};

export const branchFromContentKey = (contentKey: string) => {
return `${CMS_BRANCH_PREFIX}/${contentKey}`;
};

export interface FetchError extends Error {
status: number;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/netlify-cms-lib-util/src/APIUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const CMS_BRANCH_PREFIX = 'cms';
export const DEFAULT_PR_BODY = 'Automatically generated by Netlify CMS';
export const MERGE_COMMIT_MESSAGE = 'Automatically generated. Merged on Netlify CMS.';

const NETLIFY_CMS_LABEL_PREFIX = 'netlify-cms/';
export const isCMSLabel = (label: string) => label.startsWith(NETLIFY_CMS_LABEL_PREFIX);
export const labelToStatus = (label: string) => label.substr(NETLIFY_CMS_LABEL_PREFIX.length);
export const statusToLabel = (status: string) => `${NETLIFY_CMS_LABEL_PREFIX}${status}`;

export const generateContentKey = (collectionName: string, slug: string) =>
`${collectionName}/${slug}`;

export const parseContentKey = (contentKey: string) => {
const index = contentKey.indexOf('/');
return { collection: contentKey.substr(0, index), slug: contentKey.substr(index + 1) };
};

export const contentKeyFromBranch = (branch: string) => {
return branch.substring(`${CMS_BRANCH_PREFIX}/`.length);
};

export const branchFromContentKey = (contentKey: string) => {
return `${CMS_BRANCH_PREFIX}/${contentKey}`;
};
53 changes: 0 additions & 53 deletions packages/netlify-cms-lib-util/src/__tests__/api.spec.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,5 @@
import * as api from '../API';
describe('Api', () => {
describe('generateContentKey', () => {
it('should generate content key', () => {
expect(api.generateContentKey('posts', 'dir1/dir2/post-title')).toBe(
'posts/dir1/dir2/post-title',
);
});
});

describe('parseContentKey', () => {
it('should parse content key', () => {
expect(api.parseContentKey('posts/dir1/dir2/post-title')).toEqual({
collection: 'posts',
slug: 'dir1/dir2/post-title',
});
});
});

describe('isCMSLabel', () => {
it('should return true for CMS label', () => {
expect(api.isCMSLabel('netlify-cms/draft')).toBe(true);
});

it('should return false for non CMS label', () => {
expect(api.isCMSLabel('other/label')).toBe(false);
});
});

describe('labelToStatus', () => {
it('should get status from label', () => {
expect(api.labelToStatus('netlify-cms/draft')).toBe('draft');
});
});

describe('statusToLabel', () => {
it('should generate label from status', () => {
expect(api.statusToLabel('draft')).toBe('netlify-cms/draft');
});
});

describe('isPreviewContext', () => {
it('should return true for default preview context', () => {
expect(api.isPreviewContext('deploy', '')).toBe(true);
});

it('should return false for non default preview context', () => {
expect(api.isPreviewContext('other', '')).toBe(false);
});

it('should return true for custom preview context', () => {
expect(api.isPreviewContext('ci/custom_preview', 'ci/custom_preview')).toBe(true);
});
});

describe('getPreviewStatus', () => {
it('should return preview status on matching context', () => {
expect(api.getPreviewStatus([{ context: 'deploy' }])).toEqual({ context: 'deploy' });
Expand Down
41 changes: 41 additions & 0 deletions packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as apiUtils from '../APIUtils';
describe('APIUtils', () => {
describe('generateContentKey', () => {
it('should generate content key', () => {
expect(apiUtils.generateContentKey('posts', 'dir1/dir2/post-title')).toBe(
'posts/dir1/dir2/post-title',
);
});
});

describe('parseContentKey', () => {
it('should parse content key', () => {
expect(apiUtils.parseContentKey('posts/dir1/dir2/post-title')).toEqual({
collection: 'posts',
slug: 'dir1/dir2/post-title',
});
});
});

describe('isCMSLabel', () => {
it('should return true for CMS label', () => {
expect(apiUtils.isCMSLabel('netlify-cms/draft')).toBe(true);
});

it('should return false for non CMS label', () => {
expect(apiUtils.isCMSLabel('other/label')).toBe(false);
});
});

describe('labelToStatus', () => {
it('should get status from label', () => {
expect(apiUtils.labelToStatus('netlify-cms/draft')).toBe('draft');
});
});

describe('statusToLabel', () => {
it('should generate label from status', () => {
expect(apiUtils.statusToLabel('draft')).toBe('netlify-cms/draft');
});
});
});
16 changes: 9 additions & 7 deletions packages/netlify-cms-lib-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,25 @@ import {
import {
readFile,
readFileMetadata,
isPreviewContext,
getPreviewStatus,
PreviewState,
FetchError as FE,
ApiRequest as AR,
requestWithBackoff,
} from './API';
import {
CMS_BRANCH_PREFIX,
generateContentKey,
isCMSLabel,
labelToStatus,
statusToLabel,
DEFAULT_PR_BODY,
MERGE_COMMIT_MESSAGE,
isPreviewContext,
getPreviewStatus,
PreviewState,
FetchError as FE,
parseContentKey,
branchFromContentKey,
contentKeyFromBranch,
ApiRequest as AR,
requestWithBackoff,
} from './API';
} from './APIUtils';
import {
createPointerFile,
getLargeMediaFilteredMediaFiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CMS_BRANCH_PREFIX,
statusToLabel,
labelToStatus,
} from 'netlify-cms-lib-util/src/API';
} from 'netlify-cms-lib-util/src/APIUtils';

import { defaultSchema, joi } from '../joi';
import {
Expand Down
5 changes: 0 additions & 5 deletions packages/netlify-cms-proxy-server/src/semaphore.d.ts

This file was deleted.

0 comments on commit 49098de

Please sign in to comment.