@jungvonmatt/contentful-typings 4.1.2 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +0 -0
- package/dist/renderer/context.js +1 -1
- package/dist/renderer/context.js.map +1 -1
- package/package.json +24 -26
- package/src/index.test.ts +75 -0
- package/src/renderer/context.ts +1 -1
- package/src/renderer/renderer.test.ts +102 -0
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/renderer/context.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { pascalCase } from 'change-case';
|
|
2
2
|
import { createDefaultContext, createV10Context, } from 'cf-content-types-generator';
|
|
3
|
-
export const moduleName = (name) =>
|
|
3
|
+
export const moduleName = (name) => pascalCase(name);
|
|
4
4
|
export const moduleFieldsName = (name) => `${moduleName(name)}Fields`;
|
|
5
5
|
export const moduleSkeletonName = (name) => `${moduleName(name)}Skeleton`;
|
|
6
6
|
export const context = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/renderer/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,oBAAoB,EACpB,gBAAgB,GAEjB,MAAM,4BAA4B,CAAC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/renderer/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,oBAAoB,EACpB,gBAAgB,GAEjB,MAAM,4BAA4B,CAAC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;AAC7D,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC;AAC9E,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC;AAC1F,MAAM,CAAC,MAAM,OAAO,GAAkB;IACpC,GAAG,oBAAoB,EAAE;IACzB,UAAU;IACV,gBAAgB;IAChB,kBAAkB;CACnB,CAAC;AACF,MAAM,CAAC,MAAM,UAAU,GAAkB;IACvC,GAAG,gBAAgB,EAAE;IACrB,UAAU;IACV,gBAAgB;IAChB,kBAAkB;CACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,46 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jungvonmatt/contentful-typings",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"homepage": "https://github.com/jungvonmatt/contentful-ssg#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/jungvonmatt/contentful-ssg/issues"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"author": "",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/jungvonmatt/contentful-ssg.git"
|
|
13
|
+
},
|
|
7
14
|
"bin": {
|
|
8
|
-
"contentful-
|
|
9
|
-
"contentful-
|
|
15
|
+
"contentful-types": "./dist/cli.js",
|
|
16
|
+
"contentful-typings": "./dist/cli.js"
|
|
10
17
|
},
|
|
11
18
|
"files": [
|
|
12
19
|
"src",
|
|
13
20
|
"dist"
|
|
14
21
|
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"exports": "./dist/index.js",
|
|
15
25
|
"publishConfig": {
|
|
16
26
|
"access": "public"
|
|
17
27
|
},
|
|
18
|
-
"engines": {
|
|
19
|
-
"node": ">=20"
|
|
20
|
-
},
|
|
21
28
|
"scripts": {
|
|
22
29
|
"clean": "rimraf ./dist",
|
|
23
|
-
"test": "
|
|
24
|
-
"lint": "
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"lint": "oxlint --type-aware --fix src",
|
|
25
32
|
"precompile": "pnpm run clean",
|
|
26
33
|
"compile": "tsc --build",
|
|
27
34
|
"postcompile": "chmod +x ./dist/cli.js",
|
|
28
35
|
"watch": "tsc --build --watch"
|
|
29
36
|
},
|
|
30
|
-
"repository": {
|
|
31
|
-
"type": "git",
|
|
32
|
-
"url": "git+https://github.com/jungvonmatt/contentful-ssg.git"
|
|
33
|
-
},
|
|
34
|
-
"author": "",
|
|
35
|
-
"license": "MIT",
|
|
36
|
-
"bugs": {
|
|
37
|
-
"url": "https://github.com/jungvonmatt/contentful-ssg/issues"
|
|
38
|
-
},
|
|
39
|
-
"homepage": "https://github.com/jungvonmatt/contentful-ssg#readme",
|
|
40
37
|
"dependencies": {
|
|
41
38
|
"@faker-js/faker": "^8.3.1",
|
|
42
|
-
"@jungvonmatt/contentful-config": "
|
|
43
|
-
"@jungvonmatt/contentful-ssg": "
|
|
39
|
+
"@jungvonmatt/contentful-config": "5.0.0",
|
|
40
|
+
"@jungvonmatt/contentful-ssg": "5.0.0",
|
|
44
41
|
"cf-content-types-generator": "^2.16.0",
|
|
45
42
|
"change-case": "^5.4.2",
|
|
46
43
|
"commander": "^11.1.0",
|
|
@@ -49,16 +46,17 @@
|
|
|
49
46
|
"exit-hook": "^4.0.0",
|
|
50
47
|
"fs-extra": "^11.1.0",
|
|
51
48
|
"picocolors": "^1.0.0",
|
|
52
|
-
"prettier": "^3.2.4",
|
|
53
49
|
"read-pkg-up": "^11.0.0",
|
|
54
50
|
"semiver": "^1.1.0"
|
|
55
51
|
},
|
|
56
52
|
"devDependencies": {
|
|
57
53
|
"@types/chance": "^1.1.6",
|
|
58
54
|
"@types/fs-extra": "^11.0.4",
|
|
59
|
-
"eslint": "^8.56.0",
|
|
60
55
|
"rimraf": "^5.0.5",
|
|
61
56
|
"ts-morph": "^23.0.0"
|
|
62
57
|
},
|
|
63
|
-
"
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=22"
|
|
60
|
+
},
|
|
61
|
+
"gitHead": "dfbefe2c66877ed9411bdb242327e6915f58cf3e"
|
|
64
62
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
import { generateTypings } from './index.js';
|
|
3
|
+
|
|
4
|
+
vi.mock('@jungvonmatt/contentful-config', () => ({
|
|
5
|
+
loadContentfulConfig: vi.fn().mockResolvedValue({
|
|
6
|
+
config: {
|
|
7
|
+
managementToken: 'mt',
|
|
8
|
+
environmentId: 'master',
|
|
9
|
+
spaceId: 'space',
|
|
10
|
+
},
|
|
11
|
+
}),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
vi.mock('@jungvonmatt/contentful-ssg/lib/contentful', async (importOriginal) => {
|
|
15
|
+
const actual = (await importOriginal()) as Record<string, unknown>;
|
|
16
|
+
return {
|
|
17
|
+
...actual,
|
|
18
|
+
getEnvironment: vi.fn().mockResolvedValue({}),
|
|
19
|
+
pagedGet: vi.fn().mockResolvedValue({
|
|
20
|
+
items: [
|
|
21
|
+
{
|
|
22
|
+
sys: { id: 'page', type: 'ContentType' },
|
|
23
|
+
name: 'Page',
|
|
24
|
+
displayField: 'title',
|
|
25
|
+
description: '',
|
|
26
|
+
fields: [
|
|
27
|
+
{
|
|
28
|
+
id: 'title',
|
|
29
|
+
name: 'Title',
|
|
30
|
+
type: 'Symbol',
|
|
31
|
+
required: true,
|
|
32
|
+
localized: false,
|
|
33
|
+
omitted: false,
|
|
34
|
+
disabled: false,
|
|
35
|
+
validations: [],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
}),
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
vi.mock('read-pkg-up', () => ({
|
|
45
|
+
readPackageUp: vi.fn().mockResolvedValue({
|
|
46
|
+
packageJson: { name: 'contentful', version: '10.5.0' },
|
|
47
|
+
}),
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
describe('generateTypings', () => {
|
|
51
|
+
test('renders V10 skeleton types by default', async () => {
|
|
52
|
+
console.log = vi.fn();
|
|
53
|
+
const out = await generateTypings({});
|
|
54
|
+
expect(out).toContain('Page');
|
|
55
|
+
expect(out).toContain('Skeleton');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('legacy: true forces DefaultContentTypeRenderer', async () => {
|
|
59
|
+
console.log = vi.fn();
|
|
60
|
+
const out = await generateTypings({ legacy: true });
|
|
61
|
+
expect(out).toContain('Page');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('typeguard option emits guards', async () => {
|
|
65
|
+
console.log = vi.fn();
|
|
66
|
+
const out = await generateTypings({ typeguard: true });
|
|
67
|
+
expect(out).toContain('export function isPage');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('jsdoc + localized options work together', async () => {
|
|
71
|
+
console.log = vi.fn();
|
|
72
|
+
const out = await generateTypings({ jsdoc: true, localized: true });
|
|
73
|
+
expect(out).toContain('Page');
|
|
74
|
+
});
|
|
75
|
+
});
|
package/src/renderer/context.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
type RenderContext,
|
|
6
6
|
} from 'cf-content-types-generator';
|
|
7
7
|
|
|
8
|
-
export const moduleName = (name: string) =>
|
|
8
|
+
export const moduleName = (name: string) => pascalCase(name);
|
|
9
9
|
export const moduleFieldsName = (name: string) => `${moduleName(name)}Fields`;
|
|
10
10
|
export const moduleSkeletonName = (name: string): string => `${moduleName(name)}Skeleton`;
|
|
11
11
|
export const context: RenderContext = {
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { CFDefinitionsBuilder, type CFContentType } from 'cf-content-types-generator';
|
|
2
|
+
import { moduleName, moduleFieldsName, moduleSkeletonName } from './context.js';
|
|
3
|
+
import { DefaultContentTypeRenderer, V10ContentTypeRenderer } from './contentTypeRenderer.js';
|
|
4
|
+
import { JsDocRenderer } from './jsDocRenderer.js';
|
|
5
|
+
import { LocalizedContentTypeRenderer } from './localizedContentRenderer.js';
|
|
6
|
+
import { TypeGuardRenderer, V10TypeGuardRenderer } from './typeGuardRenderer.js';
|
|
7
|
+
|
|
8
|
+
const contentType: CFContentType = {
|
|
9
|
+
sys: { id: 'page-block', type: 'ContentType' } as CFContentType['sys'],
|
|
10
|
+
name: 'Page Block',
|
|
11
|
+
displayField: 'title',
|
|
12
|
+
description: '',
|
|
13
|
+
fields: [
|
|
14
|
+
{
|
|
15
|
+
id: 'title',
|
|
16
|
+
name: 'Title',
|
|
17
|
+
type: 'Symbol',
|
|
18
|
+
required: true,
|
|
19
|
+
localized: false,
|
|
20
|
+
omitted: false,
|
|
21
|
+
disabled: false,
|
|
22
|
+
validations: [],
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
describe('renderer/context', () => {
|
|
28
|
+
test('moduleName converts to pascal case', () => {
|
|
29
|
+
expect(moduleName('page-block')).toBe('PageBlock');
|
|
30
|
+
expect(moduleName('hero_section')).toBe('HeroSection');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('moduleFieldsName appends Fields', () => {
|
|
34
|
+
expect(moduleFieldsName('page-block')).toBe('PageBlockFields');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('moduleSkeletonName appends Skeleton', () => {
|
|
38
|
+
expect(moduleSkeletonName('page-block')).toBe('PageBlockSkeleton');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('renderer/contentTypeRenderer', () => {
|
|
43
|
+
test('DefaultContentTypeRenderer produces interfaces with PascalCase id', () => {
|
|
44
|
+
const builder = new CFDefinitionsBuilder([new DefaultContentTypeRenderer()]);
|
|
45
|
+
builder.appendType(contentType);
|
|
46
|
+
const out = builder.toString();
|
|
47
|
+
expect(out).toContain('PageBlock');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('V10ContentTypeRenderer produces skeleton types', () => {
|
|
51
|
+
const builder = new CFDefinitionsBuilder([new V10ContentTypeRenderer()]);
|
|
52
|
+
builder.appendType(contentType);
|
|
53
|
+
const out = builder.toString();
|
|
54
|
+
expect(out).toContain('PageBlock');
|
|
55
|
+
expect(out).toContain('Skeleton');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('renderer/jsDocRenderer', () => {
|
|
60
|
+
test('emits JSDoc comments referencing Pascal cased name', () => {
|
|
61
|
+
const builder = new CFDefinitionsBuilder([new V10ContentTypeRenderer(), new JsDocRenderer()]);
|
|
62
|
+
builder.appendType(contentType);
|
|
63
|
+
const out = builder.toString();
|
|
64
|
+
expect(out).toMatch(/\/\*\*[\s\S]*PageBlock/);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('renderer/localizedContentRenderer', () => {
|
|
69
|
+
test('emits localized type when included', () => {
|
|
70
|
+
const builder = new CFDefinitionsBuilder([
|
|
71
|
+
new V10ContentTypeRenderer(),
|
|
72
|
+
new LocalizedContentTypeRenderer(),
|
|
73
|
+
]);
|
|
74
|
+
builder.appendType(contentType);
|
|
75
|
+
const out = builder.toString();
|
|
76
|
+
expect(out).toContain('Localized');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('renderer/typeGuardRenderer', () => {
|
|
81
|
+
test('TypeGuardRenderer emits isPageBlock guard', () => {
|
|
82
|
+
const builder = new CFDefinitionsBuilder([
|
|
83
|
+
new DefaultContentTypeRenderer(),
|
|
84
|
+
new TypeGuardRenderer(),
|
|
85
|
+
]);
|
|
86
|
+
builder.appendType(contentType);
|
|
87
|
+
const out = builder.toString();
|
|
88
|
+
expect(out).toContain('export function isPageBlock');
|
|
89
|
+
expect(out).toContain("entry.sys.contentType.sys.id === 'page-block'");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('V10TypeGuardRenderer emits skeleton-aware guard', () => {
|
|
93
|
+
const builder = new CFDefinitionsBuilder([
|
|
94
|
+
new V10ContentTypeRenderer(),
|
|
95
|
+
new V10TypeGuardRenderer(),
|
|
96
|
+
]);
|
|
97
|
+
builder.appendType(contentType);
|
|
98
|
+
const out = builder.toString();
|
|
99
|
+
expect(out).toContain('export function isPageBlock');
|
|
100
|
+
expect(out).toContain('ChainModifiers');
|
|
101
|
+
});
|
|
102
|
+
});
|