Skip to content
This repository was archived by the owner on Jun 4, 2026. It is now read-only.

Commit 8da1f47

Browse files
fix: bumped version, added lib to git
1 parent e6cc95d commit 8da1f47

21 files changed

Lines changed: 559 additions & 2 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
*-error.log
33
/.nyc_output
44
/dist
5-
/lib
65
/tmp
76
/yarn.lock
87
node_modules

.talismanrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fileignoreconfig:
2+
- filename: lib/commands/tsgen.js
3+
checksum: 02bab20fef5508565dbf38b5c12192c48ec27249b89d48c70303abaae17984ce
4+
version: "1.0"

lib/commands/tsgen.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Command, flags } from '@contentstack/cli-command';
2+
export default class TypeScriptCodeGeneratorCommand extends Command {
3+
static description: string;
4+
static examples: string[];
5+
static flags: {
6+
'token-alias': flags.IOptionFlag<string>;
7+
output: flags.IOptionFlag<string>;
8+
prefix: flags.IOptionFlag<string>;
9+
doc: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
10+
};
11+
run(): Promise<void>;
12+
}

lib/commands/tsgen.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const cli_command_1 = require("@contentstack/cli-command");
4+
const client_1 = require("../lib/stack/client");
5+
const runner_1 = require("../lib/tsgen/runner");
6+
class TypeScriptCodeGeneratorCommand extends cli_command_1.Command {
7+
async run() {
8+
try {
9+
const { flags } = this.parse(TypeScriptCodeGeneratorCommand);
10+
const token = this.getToken(flags['token-alias']);
11+
const prefix = flags.prefix;
12+
const includeDocumentation = flags.doc;
13+
const outputPath = flags.output;
14+
if (token.type !== 'delivery') {
15+
this.warn('Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.');
16+
}
17+
if (!outputPath || !outputPath.trim()) {
18+
this.error('Please provide an output path.', { exit: 2 });
19+
}
20+
const config = {
21+
apiKey: token.apiKey,
22+
token: token.token,
23+
region: this.region.name,
24+
environment: token.environment || '',
25+
};
26+
const client = await client_1.stackConnect(this.deliveryAPIClient.Stack, config);
27+
if (client.types) {
28+
const result = await runner_1.default(outputPath, client.types, prefix, includeDocumentation);
29+
this.log(`Wrote ${result.definitions} Content Types to '${result.outputPath}'.`);
30+
}
31+
else {
32+
this.log('No Content Types exist in the Stack.');
33+
}
34+
}
35+
catch (error) {
36+
this.error(error, { exit: 1 });
37+
}
38+
}
39+
}
40+
exports.default = TypeScriptCodeGeneratorCommand;
41+
TypeScriptCodeGeneratorCommand.description = 'generate TypeScript typings from a Stack';
42+
TypeScriptCodeGeneratorCommand.examples = [
43+
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"',
44+
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" -p "I"',
45+
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --no-doc',
46+
];
47+
TypeScriptCodeGeneratorCommand.flags = {
48+
'token-alias': cli_command_1.flags.string({
49+
char: 'a',
50+
description: 'delivery token alias',
51+
hidden: false,
52+
multiple: false,
53+
required: true,
54+
}),
55+
output: cli_command_1.flags.string({
56+
char: 'o',
57+
description: 'full path to output',
58+
hidden: false,
59+
multiple: false,
60+
required: true,
61+
}),
62+
prefix: cli_command_1.flags.string({
63+
char: 'p',
64+
description: 'interface prefix, e.g. "I"',
65+
hidden: false,
66+
multiple: false,
67+
default: '',
68+
required: false,
69+
}),
70+
doc: cli_command_1.flags.boolean({
71+
char: 'd',
72+
description: 'include documentation comments',
73+
default: true,
74+
allowNo: true,
75+
}),
76+
};

lib/lib/stack/builtins.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const defaultInterfaces: (prefix?: string) => string[];

lib/lib/stack/builtins.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.defaultInterfaces = void 0;
4+
// File and Link fields are additional, non-scalar, data types within a stack.
5+
const defaultInterfaces = (prefix = '') => [
6+
`export interface ${prefix}File {
7+
uid: string;
8+
created_at: string;
9+
updated_at: string;
10+
created_by: string;
11+
updated_by: string;
12+
content_type: string;
13+
file_size: string;
14+
tags: string[];
15+
filename: string;
16+
url: string;
17+
ACL: any[];
18+
is_dir: boolean;
19+
parent_uid: string;
20+
_version: number;
21+
title: string;
22+
publish_details: {
23+
environment: string;
24+
locale: string;
25+
time: string;
26+
user: string;
27+
};
28+
}`,
29+
`export interface ${prefix}Link {
30+
title: string;
31+
href: string;
32+
}`,
33+
];
34+
exports.defaultInterfaces = defaultInterfaces;

lib/lib/stack/client.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export declare type StackConnectionConfig = {
2+
apiKey: string;
3+
token: string;
4+
region: string;
5+
environment: string;
6+
};
7+
export declare function stackConnect(client: any, config: StackConnectionConfig): Promise<{
8+
stack: any;
9+
types: any;
10+
}>;

lib/lib/stack/client.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.stackConnect = void 0;
4+
async function stackConnect(client, config) {
5+
try {
6+
// eslint-disable-next-line new-cap
7+
const stack = client(config.apiKey, config.token, config.environment, config.region);
8+
const results = await stack.getContentTypes({
9+
include_global_field_schema: true,
10+
});
11+
const types = results.content_types;
12+
if (stack) {
13+
return {
14+
stack,
15+
types,
16+
};
17+
}
18+
throw new Error('Could not connect to the stack.');
19+
}
20+
catch (error) {
21+
throw new Error('Could not connect to the stack. Please check your credentials.');
22+
}
23+
}
24+
exports.stackConnect = stackConnect;

lib/lib/stack/schema.d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export declare type Identifier = {
2+
uid: string;
3+
};
4+
export declare type Enum = {
5+
advanced: boolean;
6+
choices: Array<{
7+
value: string;
8+
}>;
9+
};
10+
export declare type FieldOptions = {
11+
display_name: string;
12+
data_type: string;
13+
mandatory: boolean;
14+
description: string;
15+
unique: boolean;
16+
multiple: boolean;
17+
non_localizable: boolean;
18+
max_instance: boolean | undefined;
19+
} & Identifier;
20+
export declare type Block = {
21+
title: string;
22+
schema: Schema;
23+
} & Identifier;
24+
export declare type GlobalField = {
25+
reference_to: string;
26+
schema: Schema;
27+
} & FieldOptions;
28+
export declare type ReferenceField = {
29+
reference_to: string | string[];
30+
} & FieldOptions;
31+
export declare type GroupField = {
32+
schema: Schema;
33+
} & FieldOptions;
34+
export declare type EnumField = {
35+
enum: Enum;
36+
} & FieldOptions;
37+
export declare type BlockField = {
38+
blocks: Block[];
39+
} & FieldOptions;
40+
export declare type Field = GlobalField & ReferenceField & GroupField & EnumField & BlockField;
41+
export declare type Schema = Array<Field>;
42+
export declare type ContentType = {
43+
description: string;
44+
schema: Schema;
45+
_version: number;
46+
} & Identifier;

lib/lib/stack/schema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

0 commit comments

Comments
 (0)