fix: move common api functions to a separate file (#3511)
This commit is contained in:
parent
816bef5ffa
commit
49098de27f
@ -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;
|
||||
}
|
||||
|
24
packages/netlify-cms-lib-util/src/APIUtils.ts
Normal file
24
packages/netlify-cms-lib-util/src/APIUtils.ts
Normal file
@ -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}`;
|
||||
};
|
@ -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' });
|
||||
|
41
packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js
Normal file
41
packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js
Normal file
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
@ -42,6 +42,14 @@ import {
|
||||
import {
|
||||
readFile,
|
||||
readFileMetadata,
|
||||
isPreviewContext,
|
||||
getPreviewStatus,
|
||||
PreviewState,
|
||||
FetchError as FE,
|
||||
ApiRequest as AR,
|
||||
requestWithBackoff,
|
||||
} from './API';
|
||||
import {
|
||||
CMS_BRANCH_PREFIX,
|
||||
generateContentKey,
|
||||
isCMSLabel,
|
||||
@ -49,16 +57,10 @@ import {
|
||||
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,
|
||||
|
@ -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 {
|
||||
|
@ -1,5 +0,0 @@
|
||||
declare module 'semaphore' {
|
||||
export type Semaphore = { take: (f: Function) => void; leave: () => void };
|
||||
const semaphore: (count: number) => Semaphore;
|
||||
export default semaphore;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user