@journeyapps/cloudcode-build
Advanced tools
Comparing version 1.9.7-dev.00b908a.e8c9b44 to 1.9.7-dev.4d0aca6.c25ef77
import { Type, Schema } from '@journeyapps/parser-schema'; | ||
export declare function mapType(type: Type): any; | ||
/** | ||
* Given a Journey type, return a TypeScript type name. | ||
* | ||
* If the type is unknown, we return 'any'. | ||
*/ | ||
export declare function mapType(type: Type | null): any; | ||
export declare function schemaToDefinitions(schema: Schema): string; | ||
/** | ||
* Get a reference to a model type/interface. | ||
* | ||
* "user" => "DB.user". | ||
* "class" => DB._class". | ||
* | ||
* @param modelName Name of the model. | ||
*/ | ||
export declare function typeRef(modelName: string): string; | ||
/** | ||
* Given a model name, return a safe identifier for the model interface. | ||
* | ||
* Generally the interface name should equal the model name. However, in some cases the model name is a reserved | ||
* keyword, in which case we prepend an underscore. | ||
* | ||
* "user" => "user" | ||
* "class" => "_class". | ||
* | ||
* @param name Name of the model. | ||
*/ | ||
export declare function modelInterfaceName(name: string): string; | ||
/** | ||
* Returns true if an identifier is valid as a property name in JavaScript / TypeScript. | ||
* In other words, it can be directly used as obj.prop_name_here, versus the workaround of obj["prop name here"]. | ||
* | ||
* General restrictions are that it cannot start with a number, and cannot contain spaces or special characters. | ||
* @param name - The identifier | ||
*/ | ||
export declare function isValidProperty(name: string): boolean; | ||
/** | ||
* Return a property name safe to embed in a TS definition file. | ||
* | ||
* Where feasible, we return the property directly. If it's not a valid name directly, we wrap it in quotes. | ||
* | ||
* my_prop123 => my_prop123 | ||
* 1time => "1time" | ||
* multiple words => "multiple words" | ||
* | ||
* @param name - The property name. | ||
*/ | ||
export declare function safeProperty(name: string): string; | ||
export declare const CONTEXTUAL_KEYWORDS: string[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const reservedKeywords_1 = require("./reservedKeywords"); | ||
const TYPE_MAP = { | ||
@@ -18,4 +19,16 @@ text: 'string', | ||
}; | ||
function isArrayType(type) { | ||
return type.name == 'array'; | ||
} | ||
function isQueryType(type) { | ||
return type.name == 'query'; | ||
} | ||
/** | ||
* Given a Journey type, return a TypeScript type name. | ||
* | ||
* If the type is unknown, we return 'any'. | ||
*/ | ||
function mapType(type) { | ||
if (type == null) { | ||
// Safety fallback. | ||
return 'any'; | ||
@@ -26,2 +39,11 @@ } | ||
} | ||
else if (type.isObject) { | ||
return typeRef(type.name); | ||
} | ||
else if (isArrayType(type) && type.objectType) { | ||
return `${typeRef(type.objectType.name)}[]`; | ||
} | ||
else if (isQueryType(type) && type.objectType) { | ||
return `Query<${typeRef(type.objectType.name)}>`; | ||
} | ||
else { | ||
@@ -64,6 +86,6 @@ return 'any'; | ||
let attrs = []; | ||
for (let attributeName in model.attributes) { | ||
for (let attributeName of Object.keys(model.attributes)) { | ||
let attribute = model.attributes[attributeName]; | ||
attrs.push(`/** ${attribute.label} (${attribute.sourceTypeName}) */`, `${safeProperty(attributeName)}: ${mapType(attribute.type)};`); | ||
if (attribute.type.name == 'attachment') { | ||
if (attribute.type && attribute.type.name == 'attachment') { | ||
attrs.push(`/** ID of ${attribute.label}. */`, `readonly ${attributeName}_id: string;`); | ||
@@ -74,2 +96,5 @@ } | ||
let rel = model.belongsTo[relName]; | ||
if (!rel.foreignType) { | ||
continue; | ||
} | ||
attrs.push(`/** Lookup related ${rel.foreignType.label}. */`, `${safeProperty(relName)}(): Promise<${typeRef(rel.foreignType.name)}>;`, `/** Set related ${rel.foreignType.label}. */`, `${safeProperty(relName)}(value: ${typeRef(rel.foreignType.name)}): void;`, `/** ID of related ${rel.foreignType.label}. */`, `${safeProperty(relName + '_id')}: string;`); | ||
@@ -79,2 +104,5 @@ } | ||
let rel = model.hasMany[relName]; | ||
if (!rel.objectType) { | ||
continue; | ||
} | ||
attrs.push(`/** Query for related ${rel.objectType.label}. */`, `readonly ${safeProperty(relName)}: Query<${typeRef(rel.objectType.name)}>;`); | ||
@@ -113,2 +141,10 @@ } | ||
exports.schemaToDefinitions = schemaToDefinitions; | ||
/** | ||
* Get a reference to a model type/interface. | ||
* | ||
* "user" => "DB.user". | ||
* "class" => DB._class". | ||
* | ||
* @param modelName Name of the model. | ||
*/ | ||
function typeRef(modelName) { | ||
@@ -118,2 +154,13 @@ return `DB.${modelInterfaceName(modelName)}`; | ||
exports.typeRef = typeRef; | ||
/** | ||
* Given a model name, return a safe identifier for the model interface. | ||
* | ||
* Generally the interface name should equal the model name. However, in some cases the model name is a reserved | ||
* keyword, in which case we prepend an underscore. | ||
* | ||
* "user" => "user" | ||
* "class" => "_class". | ||
* | ||
* @param name Name of the model. | ||
*/ | ||
function modelInterfaceName(name) { | ||
@@ -123,3 +170,3 @@ // If the name is a reserved word, we prepend an underscore to the type name. | ||
// Effectively we'd get `var c: DB._class = DB.class.create()` | ||
if (TYPE_BLACKLIST[name]) { | ||
if (reservedKeywords_1.isReservedTypeName(name)) { | ||
return `_${name}`; | ||
@@ -131,2 +178,9 @@ } | ||
const PROPERTY_REGEXP = /^(?![0-9])[a-zA-Z0-9$_]+$/; | ||
/** | ||
* Returns true if an identifier is valid as a property name in JavaScript / TypeScript. | ||
* In other words, it can be directly used as obj.prop_name_here, versus the workaround of obj["prop name here"]. | ||
* | ||
* General restrictions are that it cannot start with a number, and cannot contain spaces or special characters. | ||
* @param name - The identifier | ||
*/ | ||
function isValidProperty(name) { | ||
@@ -136,2 +190,13 @@ return PROPERTY_REGEXP.test(name); | ||
exports.isValidProperty = isValidProperty; | ||
/** | ||
* Return a property name safe to embed in a TS definition file. | ||
* | ||
* Where feasible, we return the property directly. If it's not a valid name directly, we wrap it in quotes. | ||
* | ||
* my_prop123 => my_prop123 | ||
* 1time => "1time" | ||
* multiple words => "multiple words" | ||
* | ||
* @param name - The property name. | ||
*/ | ||
function safeProperty(name) { | ||
@@ -148,79 +213,2 @@ // We may get invalid property names. In that case the datamodel should have validation errors, but we still | ||
exports.safeProperty = safeProperty; | ||
// From: https://github.com/Microsoft/TypeScript/issues/2536 | ||
const RESERVED_WORDS = [ | ||
'break', | ||
'case', | ||
'catch', | ||
'class', | ||
'const', | ||
'continue', | ||
'debugger', | ||
'default', | ||
'delete', | ||
'do', | ||
'else', | ||
'enum', | ||
'export', | ||
'extends', | ||
'false', | ||
'finally', | ||
'for', | ||
'function', | ||
'if', | ||
'import', | ||
'in', | ||
'instanceof', | ||
'new', | ||
'null', | ||
'return', | ||
'super', | ||
'switch', | ||
'this', | ||
'throw', | ||
'true', | ||
'try', | ||
'typeof', | ||
'var', | ||
'void', | ||
'while', | ||
'with' | ||
]; | ||
// These currently appear to work fine as interface names. | ||
// Listed here for reference only | ||
const STRICT_RESERVED_WORDS = [ | ||
'as', | ||
'implements', | ||
'interface', | ||
'let', | ||
'package', | ||
'private', | ||
'protected', | ||
'public', | ||
'static', | ||
'yield' | ||
]; | ||
// Some of these appear to work as interface names, but we blacklist them all. | ||
exports.CONTEXTUAL_KEYWORDS = [ | ||
'any', | ||
'boolean', | ||
'constructor', | ||
'declare', | ||
'get', | ||
'module', | ||
'require', | ||
'number', | ||
'set', | ||
'string', | ||
'symbol', | ||
'type', | ||
'from', | ||
'of' | ||
]; | ||
const TYPE_BLACKLIST = {}; | ||
for (let key of exports.CONTEXTUAL_KEYWORDS) { | ||
TYPE_BLACKLIST[key] = true; | ||
} | ||
for (let key of RESERVED_WORDS) { | ||
TYPE_BLACKLIST[key] = true; | ||
} | ||
//# sourceMappingURL=datamodel.js.map |
@@ -1,5 +0,6 @@ | ||
import { schemaToDefinitions } from './datamodel'; | ||
import { schemaToDefinitions, mapType, typeRef } from './datamodel'; | ||
import { buildTypescript } from './build'; | ||
import { ProcessError } from './ProcessError'; | ||
export { buildTypescript, ProcessError, schemaToDefinitions }; | ||
export { buildTypescript, ProcessError, schemaToDefinitions, mapType, typeRef }; | ||
export { isReservedTypeName } from './reservedKeywords'; | ||
export interface BuildOptions { | ||
@@ -6,0 +7,0 @@ datamodel: string; |
@@ -5,2 +5,4 @@ "use strict"; | ||
exports.schemaToDefinitions = datamodel_1.schemaToDefinitions; | ||
exports.mapType = datamodel_1.mapType; | ||
exports.typeRef = datamodel_1.typeRef; | ||
const build_1 = require("./build"); | ||
@@ -10,2 +12,4 @@ exports.buildTypescript = build_1.buildTypescript; | ||
exports.ProcessError = ProcessError_1.ProcessError; | ||
var reservedKeywords_1 = require("./reservedKeywords"); | ||
exports.isReservedTypeName = reservedKeywords_1.isReservedTypeName; | ||
const path = require("path"); | ||
@@ -12,0 +16,0 @@ const fs = require("fs"); |
{ | ||
"name": "@journeyapps/cloudcode-build", | ||
"version": "1.9.7-dev.00b908a.e8c9b44", | ||
"version": "1.9.7-dev.4d0aca6.c25ef77", | ||
"main": "./lib/index.js", | ||
@@ -32,3 +32,3 @@ "license": "MIT", | ||
], | ||
"gitHead": "2b895f3c167e7d944d635ef06f096eadb4ec2be8" | ||
"gitHead": "71a4de0855618becf26efeadfe4cf173259a1640" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26262
21
494