-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move common api functions to a separate file (#3511)
- Loading branch information
Showing
7 changed files
with
75 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.