Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function generate(inPath) {
// Best description of how to use the API was found at
// https://blog.quicktype.io/customizing-quicktype/
const inputData = new InputData();
const content = fs.readFileSync(inPath, 'utf-8');
const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };
await inputData.addSource('schema', source, () => {
return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
});
const lang = new TypeScriptTargetLanguage();
const { lines } = await quicktype({
lang,
inputData,
alphabetizeProperties: true,
rendererOptions: {
'just-types': 'true',
'explicit-unions': 'true',
const configuration = vscode.workspace.getConfiguration(configurationSection);
const justTypes = forceJustTypes || configuration.justTypes;
const rendererOptions: RendererOptions = {};
if (justTypes) {
// FIXME: The target language should have a property to return these options.
if (lang.name === "csharp") {
rendererOptions["features"] = "just-types";
} else if (lang.name === "kotlin") {
rendererOptions["framework"] = "just-types";
} else {
rendererOptions["just-types"] = "true";
}
}
const inputData = new InputData();
switch (kind) {
case "json":
await inputData.addSource("json", { name: topLevelName, samples: [content] }, () =>
jsonInputForTargetLanguage(lang)
);
break;
case "schema":
await inputData.addSource(
"schema",
{ name: topLevelName, schema: content },
() => new JSONSchemaInput(undefined)
);
break;
case "typescript":
await inputData.addSource(
"schema",
async function generate(inPath) {
// Best description of how to use the API was found at
// https://blog.quicktype.io/customizing-quicktype/
const inputData = new InputData();
const content = fs.readFileSync(inPath, 'utf-8');
const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };
await inputData.addSource('schema', source, () => {
return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
});
const lang = new TypeScriptTargetLanguage();
const { lines } = await quicktype({
lang,
inputData,
alphabetizeProperties: true,
rendererOptions: {
'just-types': 'true',
'explicit-unions': 'true',
'acronym-style': 'camel',
},
});
return header + lines.join('\n') + footer;
}
this.forEachTopLevel('leading-and-interposing', (t, name) => {
const camelCaseName = modifySource(camelCase, name)
this.emitDescription(this.descriptionForType(t))
// Override sourceFor behavior to disable `{ [key: string]: any }`
// if no properties are set for an event.
let type = this.sourceFor(t).source
if (t instanceof MapType) {
type = '{}'
}
if (this.ajsOptions.client === Client.js) {
this.emitLine([
camelCaseName,
'(props?: ',
type,
', options?: SegmentOptions, callback?: AnalyticsJSCallback): void'
])
withOptions: boolean
): void {
// TODO: Emit a function description, once we support top-level event descriptions in JSON Schema
const description: Sourcelike = [
['@see <a href="https://segment.com/docs/spec/track/">Track Documentation</a>']
]
if (hasProperties) {
description.unshift([
'@param props {@link ',
name,
'} to add extra information to this call.'
])
}
this.emitDescriptionBlock(description)
const camelCaseName = modifySource(camelCase, name)
this.emitBlock(
[
'public void ',
camelCaseName,
'(',
...(hasProperties ? ['final @Nullable ', name, ' props'] : []),
hasProperties && withOptions ? ', ' : '',
withOptions ? 'final @Nullable Options options' : '',
')'
],
() => {
this.emitLine([
'this.analytics.track("',
getRawName(name),
'", ',
hasProperties ? 'props.toProperties()' : 'new Properties()',
async function generate(inPath) {
// Best description of how to use the API was found at
// https://blog.quicktype.io/customizing-quicktype/
const inputData = new InputData();
const content = fs.readFileSync(inPath, 'utf-8');
const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };
await inputData.addSource('schema', source, () => {
return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
});
const lang = new TypeScriptTargetLanguage();
const { lines } = await quicktype({
lang,
inputData,
alphabetizeProperties: true,
rendererOptions: {
'just-types': 'true',
'explicit-unions': 'true',
'acronym-style': 'camel',
},
});
return header + lines.join('\n') + footer;
}
openForEditor(editor, "json")
),
vscode.commands.registerTextEditorCommand(Command.OpenQuicktypeForJSONSchema, editor =>
openForEditor(editor, "schema")
),
vscode.commands.registerTextEditorCommand(Command.OpenQuicktypeForTypeScript, editor =>
openForEditor(editor, "typescript")
),
vscode.commands.registerCommand(Command.ChangeTargetLanguage, changeTargetLanguage)
);
await persist.init({ dir: path.join(homedir(), ".quicktype-vscode") });
const maybeName = await persist.getItem(lastTargetLanguageUsedKey);
if (typeof maybeName === "string") {
explicitlySetTargetLanguage = languageNamed(maybeName);
}
}
export async function genTS(events: TrackedEvent[]) {
const inputData = new InputData()
events.forEach(({ name, rules }) => {
const schema = {
$schema: 'http://json-schema.org/draft-04/schema#',
title: rules.title,
description: rules.description,
...rules.properties.properties
}
inputData.addSource(
'schema',
{ name, uris: [name], schema: JSON.stringify(schema) },
() => new JSONSchemaInput(undefined)
)
})
export const processEventsForQuickType = (events: TrackedEvent[]) => {
const inputData = new InputData()
events.forEach(({ name, rules }) => {
const schema = {
$schema: rules.$schema || 'http://json-schema.org/draft-07/schema#',
title: rules.title,
description: rules.description,
...get(rules, 'properties.properties', {})
}
inputData.addSource(
'schema',
{ name, uris: [name], schema: JSON.stringify(schema) },
() => new JSONSchemaInput(undefined)
)
})
augmentModule: boolean,
leadingComments: string[] | undefined,
rendererOptions: RendererOptions,
) => {
const src: JSONSchemaSourceData = {
name,
schema: JSON.stringify(schema),
};
class FetchingJSONSchemaStore extends JSONSchemaStore {
async fetch(address: string): Promise {
return (schemaRefs as any)[address];
}
}
const inputData = new InputData();
await inputData.addSource(
'schema',
src,
() => new JSONSchemaInput(new FetchingJSONSchemaStore(), []),
);
const { lines } = await quicktype({
inputData,
lang: type,
indentation: ' ',
leadingComments: (leadingComments || [
'AUTO GENERATED CODE',
'Run app-config with \'generate\' command to regenerate this file',
]),
rendererOptions: {
'just-types': 'true',