@teleporthq/teleport-plugin-common
Advanced tools
Comparing version
@@ -202,51 +202,31 @@ "use strict"; | ||
var _a = node.content.nodes, success = _a.success, empty = _a.empty, error = _a.error, loading = _a.loading; | ||
var _b = node.content, loopItemsReference = _b.loopItemsReference, statePersistanceName = _b.statePersistanceName; | ||
var type = loopItemsReference.type; | ||
var errorNodeAST = null; | ||
var emptyNodeAST = null; | ||
var loadingNodeAST = null; | ||
var listNodeAST = null; | ||
var source = (0, utils_1.getRepeatSourceIdentifier)(node.content.loopItemsReference, options); | ||
/* | ||
* TODO: @JK move this too into validator | ||
* CMS list node can only be a dynamic !! | ||
*/ | ||
if (type !== 'dynamic') { | ||
throw new Error("Node ".concat(node, " is dynamic, but the referece link is missing. \n\n Missing loopItemsReference")); | ||
} | ||
var statePersistanceName = node.content.statePersistanceName; | ||
var loadingStatePersistanceName = teleport_shared_1.StringUtils.createStateOrPropStoringValue("".concat(statePersistanceName, "Loading")); | ||
var errorStatePersistanceName = teleport_shared_1.StringUtils.createStateOrPropStoringValue("".concat(statePersistanceName, "Error")); | ||
if (empty) { | ||
emptyNodeAST = generateNode(empty, params, options)[0]; | ||
} | ||
if (error) { | ||
errorNodeAST = generateNode(error, params, options)[0]; | ||
params.stateDefinitions["".concat(statePersistanceName, "Error")] = { | ||
type: 'boolean', | ||
defaultValue: false, | ||
}; | ||
} | ||
if (loading) { | ||
loadingNodeAST = generateNode(loading, params, options)[0]; | ||
params.stateDefinitions["".concat(statePersistanceName, "Loading")] = { | ||
type: 'boolean', | ||
defaultValue: false, | ||
}; | ||
} | ||
if (success) { | ||
listNodeAST = generateNode(success, params, options)[0]; | ||
} | ||
var listNodeAST = !!success | ||
? generateNode(success, params, options)[0] | ||
: null; | ||
var source = (0, utils_1.getRepeatSourceIdentifier)(node.content.loopItemsReference, options); | ||
var emptyNodeAST = !!empty | ||
? generateNode(empty, params, options)[0] | ||
: null; | ||
var emptyNodeExpressionAST = empty | ||
? types.logicalExpression('&&', types.unaryExpression('!', types.memberExpression(source, types.identifier('length'))), emptyNodeAST) | ||
: null; | ||
var errorNodeAST = !!error | ||
? generateNode(error, params, options)[0] | ||
: null; | ||
var errorNodeExpressionAST = error && errorStatePersistanceName | ||
? types.logicalExpression('&&', types.identifier(errorStatePersistanceName), errorNodeAST) | ||
: null; | ||
var loadingNodeAST = !!loading | ||
? generateNode(loading, params, options)[0] | ||
: null; | ||
var loadingNodeExpressionAST = loading && loadingStatePersistanceName | ||
? types.logicalExpression('&&', types.identifier(loadingStatePersistanceName), loadingNodeAST) | ||
: null; | ||
var _c = teleport_shared_1.UIDLUtils.getRepeatIteratorNameAndKey({ | ||
var _b = teleport_shared_1.UIDLUtils.getRepeatIteratorNameAndKey({ | ||
useIndex: true, | ||
iteratorName: 'item', | ||
}), iteratorName = _c.iteratorName, iteratorKey = _c.iteratorKey; | ||
}), iteratorName = _b.iteratorName, iteratorKey = _b.iteratorKey; | ||
var localIteratorPrefix = options.dynamicReferencePrefixMap.local; | ||
@@ -253,0 +233,0 @@ (0, ast_utils_1.addDynamicAttributeToJSXTag)(listNodeAST, 'key', iteratorKey, localIteratorPrefix); |
import * as types from '@babel/types'; | ||
import ParsedASTNode from './parsed-ast'; | ||
import { UIDLStateDefinition, UIDLPropDefinition, UIDLRawValue, UIDLStaticValue, UIDLDynamicReference, UIDLResourceItem, UIDLPropValue, UIDLExpressionValue } from '@teleporthq/teleport-types'; | ||
import { UIDLStateDefinition, UIDLPropDefinition, UIDLRawValue, Resource, UIDLDynamicReference } from '@teleporthq/teleport-types'; | ||
import { JSXGenerationOptions, JSXGenerationParams } from '../node-handlers/node-to-jsx/types'; | ||
@@ -58,7 +58,6 @@ /** | ||
export declare const wrapObjectPropertiesWithExpression: (properties: types.ObjectProperty[]) => types.ObjectExpression; | ||
export declare const generateRemoteResourceASTs: (resource: UIDLResourceItem) => types.VariableDeclaration[]; | ||
export declare const generateRemoteResourceASTs: (resource: Resource, propsPrefix?: string, extraUrlParamsGenerator?: () => types.ObjectProperty[]) => types.VariableDeclaration[]; | ||
export declare const generateMemberExpressionASTFromBase: (base: types.MemberExpression | types.Identifier, path: string[]) => types.MemberExpression; | ||
export declare const generateMemberExpressionASTFromPath: (path: Array<string | number>) => types.MemberExpression | types.Identifier; | ||
export declare const resolveObjectValue: (prop: UIDLStaticValue | UIDLPropValue | UIDLExpressionValue) => types.BooleanLiteral | types.Identifier | types.NumericLiteral | types.StringLiteral; | ||
export declare const generateMemberExpressionASTFromPath: (path: string[]) => types.MemberExpression | types.Identifier; | ||
export {}; | ||
//# sourceMappingURL=ast-utils.d.ts.map |
@@ -38,3 +38,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resolveObjectValue = exports.generateMemberExpressionASTFromPath = exports.generateMemberExpressionASTFromBase = exports.generateRemoteResourceASTs = exports.wrapObjectPropertiesWithExpression = exports.generateDynamicWindowImport = exports.createStateHookAST = exports.createReturnExpressionSyntax = exports.createPureComponent = exports.createClassComponent = exports.removeAttributeByName = exports.findAttributeByName = exports.getTSAnnotationForType = exports.addPropertyToASTObject = exports.convertValueToLiteral = exports.objectToObjectExpression = exports.renameJSXTag = exports.addSpreadAttributeToJSXTag = exports.addChildJSXText = exports.addChildJSXTag = exports.addRawAttributeToJSXTag = exports.addAttributeToJSXTag = exports.stringAsTemplateLiteral = exports.addMultipleDynamicAttributesToJSXTag = exports.addDynamicExpressionAttributeToJSXTag = exports.addDynamicCtxAttributeToJSXTag = exports.addDynamicAttributeToJSXTag = exports.addClassStringOnJSXTag = void 0; | ||
exports.generateMemberExpressionASTFromPath = exports.generateMemberExpressionASTFromBase = exports.generateRemoteResourceASTs = exports.wrapObjectPropertiesWithExpression = exports.generateDynamicWindowImport = exports.createStateHookAST = exports.createReturnExpressionSyntax = exports.createPureComponent = exports.createClassComponent = exports.removeAttributeByName = exports.findAttributeByName = exports.getTSAnnotationForType = exports.addPropertyToASTObject = exports.convertValueToLiteral = exports.objectToObjectExpression = exports.renameJSXTag = exports.addSpreadAttributeToJSXTag = exports.addChildJSXText = exports.addChildJSXTag = exports.addRawAttributeToJSXTag = exports.addAttributeToJSXTag = exports.stringAsTemplateLiteral = exports.addMultipleDynamicAttributesToJSXTag = exports.addDynamicExpressionAttributeToJSXTag = exports.addDynamicCtxAttributeToJSXTag = exports.addDynamicAttributeToJSXTag = exports.addClassStringOnJSXTag = void 0; | ||
var types = __importStar(require("@babel/types")); | ||
@@ -45,3 +45,2 @@ var core_1 = require("@babel/core"); | ||
var utils_1 = require("../node-handlers/node-to-jsx/utils"); | ||
var __1 = require(".."); | ||
/** | ||
@@ -108,6 +107,2 @@ * Adds a class definition string to an existing string of classes | ||
if (t === void 0) { t = types; } | ||
// TODO: Fix !! | ||
if (!value) { | ||
return; | ||
} | ||
var content = prefix === '' | ||
@@ -447,15 +442,10 @@ ? t.identifier(value) | ||
exports.wrapObjectPropertiesWithExpression = wrapObjectPropertiesWithExpression; | ||
/* | ||
* TODO: Add the ability to support body and payload. | ||
* UIDLResourceItem['body'] | ||
* All the dynamic props are sent using the function props | ||
*/ | ||
var generateRemoteResourceASTs = function (resource) { | ||
var generateRemoteResourceASTs = function (resource, propsPrefix, extraUrlParamsGenerator) { | ||
if (propsPrefix === void 0) { propsPrefix = ''; } | ||
var fetchUrl = computeFetchUrl(resource); | ||
var authHeaderAST = computeAuthorizationHeaderAST(resource === null || resource === void 0 ? void 0 : resource.headers); | ||
var headersASTs = generateRESTHeadersAST(resource === null || resource === void 0 ? void 0 : resource.headers); | ||
var queryParams = generateURLParamsAST(resource === null || resource === void 0 ? void 0 : resource.params); | ||
var authHeaderAST = computeAuthorizationHeaderAST(resource); | ||
var queryParams = generateURLParamsAST(resource.urlParams, propsPrefix, extraUrlParamsGenerator); | ||
var fetchUrlQuasis = fetchUrl.quasis; | ||
var queryParamsQuasis = (queryParams === null || queryParams === void 0 ? void 0 : queryParams.quasis) || [types.templateElement({ raw: '', cooked: '' })]; | ||
if ((queryParams === null || queryParams === void 0 ? void 0 : queryParams.expressions.length) > 0) { | ||
var queryParamsQuasis = queryParams.quasis; | ||
if (queryParams.expressions.length > 0) { | ||
fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw = | ||
@@ -467,18 +457,3 @@ fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw + '?'; | ||
} | ||
var paramsDeclerations = Object.keys((resource === null || resource === void 0 ? void 0 : resource.params) || {}).reduce(function (acc, item) { | ||
var prop = resource.params[item]; | ||
if (prop.type === 'static') { | ||
acc.push(types.objectProperty(types.stringLiteral(item), __1.ASTUtils.resolveObjectValue(prop))); | ||
} | ||
if (prop.type === 'dynamic') { | ||
acc.push(types.spreadElement(types.logicalExpression('&&', types.memberExpression(types.identifier('params'), types.stringLiteral(item), true, false), types.objectExpression([ | ||
types.objectProperty(types.stringLiteral(item), types.memberExpression(types.identifier('params'), types.stringLiteral(item), true, false)), | ||
])))); | ||
} | ||
return acc; | ||
}, []); | ||
var paramsAST = types.variableDeclaration('const', [ | ||
types.variableDeclarator(types.identifier('urlParams'), types.objectExpression(paramsDeclerations)), | ||
]); | ||
var url = (queryParams === null || queryParams === void 0 ? void 0 : queryParams.quasis) | ||
var url = queryParams | ||
? types.templateLiteral(__spreadArray(__spreadArray([], fetchUrlQuasis, true), queryParamsQuasis, true), __spreadArray([], fetchUrl.expressions.concat(queryParams.expressions), true)) | ||
@@ -490,4 +465,6 @@ : fetchUrl; | ||
types.objectExpression([ | ||
types.objectProperty(types.identifier('method'), types.stringLiteral(resource.method)), | ||
types.objectProperty(types.identifier('headers'), types.objectExpression(__spreadArray(__spreadArray([], headersASTs, true), [authHeaderAST], false))), | ||
types.objectProperty(types.identifier('headers'), types.objectExpression([ | ||
types.objectProperty(types.identifier('"Content-Type"'), types.stringLiteral('application/json'), false, false), | ||
authHeaderAST, | ||
])), | ||
]), | ||
@@ -499,12 +476,5 @@ ]))), | ||
]); | ||
return [paramsAST, fetchAST, responseJSONAST]; | ||
return [fetchAST, responseJSONAST]; | ||
}; | ||
exports.generateRemoteResourceASTs = generateRemoteResourceASTs; | ||
var generateRESTHeadersAST = function (headers) { | ||
return Object.keys(headers) | ||
.filter(function (header) { return header !== 'authToken'; }) | ||
.map(function (header) { | ||
return types.objectProperty(types.stringLiteral(header), types.stringLiteral(String(headers[header].content))); | ||
}); | ||
}; | ||
var generateMemberExpressionASTFromBase = function (base, path) { | ||
@@ -522,44 +492,63 @@ if (path.length === 1) { | ||
if (path.length === 1) { | ||
return types.identifier(path[0].toString()); | ||
return types.identifier(path[0]); | ||
} | ||
pathClone.pop(); | ||
var currentPath = path[path.length - 1]; | ||
if (typeof currentPath === 'number') { | ||
return types.memberExpression((0, exports.generateMemberExpressionASTFromPath)(pathClone), types.numericLiteral(currentPath), true); | ||
} | ||
var containsSpecial = currentPath.indexOf('.') !== -1 || currentPath.indexOf('-') !== -1; | ||
return types.memberExpression((0, exports.generateMemberExpressionASTFromPath)(pathClone), containsSpecial ? types.stringLiteral(currentPath) : types.identifier(currentPath), containsSpecial); | ||
return types.memberExpression((0, exports.generateMemberExpressionASTFromPath)(pathClone), types.identifier(path[path.length - 1]), false); | ||
}; | ||
exports.generateMemberExpressionASTFromPath = generateMemberExpressionASTFromPath; | ||
var generateURLParamsAST = function (urlParams) { | ||
if (!urlParams) { | ||
return null; | ||
} | ||
var generateURLParamsAST = function (urlParams, propsPrefix, extraUrlParamsGenerator) { | ||
var queryString = {}; | ||
Object.keys(urlParams).forEach(function (key) { | ||
resolveDynamicValuesFromUrlParams(urlParams[key], queryString, key); | ||
resolveDynamicValuesFromUrlParams(urlParams[key], queryString, key, propsPrefix); | ||
}); | ||
var urlObject = types.objectExpression(__spreadArray(__spreadArray([], Object.keys(queryString).map(function (key) { | ||
return types.objectProperty(types.stringLiteral("".concat(key)), queryString[key]); | ||
}), true), (extraUrlParamsGenerator ? extraUrlParamsGenerator() : []), true)); | ||
return types.templateLiteral([ | ||
types.templateElement({ raw: '', cooked: '' }, false), | ||
types.templateElement({ raw: '', cooked: '' }, true), | ||
], [types.newExpression(types.identifier('URLSearchParams'), [types.identifier('urlParams')])]); | ||
], [types.newExpression(types.identifier('URLSearchParams'), [urlObject])]); | ||
}; | ||
var resolveDynamicValuesFromUrlParams = function (field, query, prefix) { | ||
var resolveDynamicValuesFromUrlParams = function (field, query, prefix, propsPrefix) { | ||
if (prefix === void 0) { prefix = null; } | ||
if (field.type === 'dynamic' || field.type === 'static') { | ||
query[prefix] = resolveUrlParamsValue(field); | ||
if (propsPrefix === void 0) { propsPrefix = ''; } | ||
if (Array.isArray(field)) { | ||
var arrayValues = field.map(function (value) { | ||
return resolveUrlParamsValue(value, propsPrefix); | ||
}); | ||
query[prefix] = types.arrayExpression(arrayValues); | ||
return; | ||
} | ||
if (field.type === 'dynamic' || field.type === 'static' || field.type === 'expr') { | ||
query[prefix] = resolveUrlParamsValue(field, propsPrefix); | ||
return; | ||
} | ||
Object.keys(field).forEach(function (key) { | ||
var value = field[key]; | ||
var newPrefix = prefix ? "".concat(prefix, "[").concat(key, "]") : key; | ||
if (typeof value === 'object') { | ||
resolveDynamicValuesFromUrlParams(value, query, newPrefix, propsPrefix); | ||
return; | ||
} | ||
query[newPrefix] = resolveUrlParamsValue(value, propsPrefix); | ||
}); | ||
}; | ||
var resolveUrlParamsValue = function (urlParam) { | ||
if (urlParam.type === 'static') { | ||
return types.stringLiteral("".concat(urlParam.content)); | ||
var resolveUrlParamsValue = function (urlParams, propsPrefix) { | ||
if (propsPrefix === void 0) { propsPrefix = ''; } | ||
if (urlParams.type === 'static') { | ||
return types.stringLiteral("".concat(urlParams.content)); | ||
} | ||
if (urlParam.content.referenceType !== 'prop') { | ||
if (urlParams.type === 'expr') { | ||
return types.identifier(urlParams.content); | ||
} | ||
if (urlParams.content.referenceType !== 'prop') { | ||
throw new Error('Only prop references are supported for url params'); | ||
} | ||
var paramPath = ['params', urlParam.content.id]; | ||
var paramPath = __spreadArray(__spreadArray([], (propsPrefix ? [propsPrefix] : []), true), (urlParams.content.path || []), true); | ||
var templateLiteralElements = paramPath | ||
.map(function (_, index) { | ||
var isTail = index === paramPath.length - 1; | ||
if (index === paramPath.length - 1) { | ||
return null; | ||
} | ||
var isTail = index === paramPath.length - 2; | ||
return types.templateElement({ | ||
@@ -575,9 +564,9 @@ cooked: '', | ||
}; | ||
var computeAuthorizationHeaderAST = function (headers) { | ||
var computeAuthorizationHeaderAST = function (resource) { | ||
var _a; | ||
var authToken = resolveResourceValue(headers.authToken); | ||
var authToken = resolveResourceValue(resource.authToken); | ||
if (!authToken) { | ||
return null; | ||
} | ||
var authTokenType = (_a = headers.authToken) === null || _a === void 0 ? void 0 : _a.type; | ||
var authTokenType = (_a = resource.authToken) === null || _a === void 0 ? void 0 : _a.type; | ||
return types.objectProperty(types.identifier('Authorization'), types.templateLiteral(__spreadArray([ | ||
@@ -595,11 +584,10 @@ types.templateElement({ | ||
}, true), | ||
]), true), __spreadArray([], (authTokenType === 'static' ? [] : [types.identifier(String(authToken))]), true)), false, false); | ||
]), true), __spreadArray([], (authTokenType === 'static' ? [] : [types.identifier(authToken)]), true)), false, false); | ||
}; | ||
var computeFetchUrl = function (resource) { | ||
var _a, _b; | ||
var path = resource.path; | ||
var fetchBaseUrl = resolveResourceValue(path.baseUrl); | ||
var resourceRoute = resolveResourceValue(path.route); | ||
var baseUrlType = (_a = path.baseUrl) === null || _a === void 0 ? void 0 : _a.type; | ||
var routeType = (_b = path.route) === null || _b === void 0 ? void 0 : _b.type; | ||
var fetchBaseUrl = resolveResourceValue(resource.baseUrl); | ||
var resourceRoute = resolveResourceValue(resource.route); | ||
var baseUrlType = (_a = resource.baseUrl) === null || _a === void 0 ? void 0 : _a.type; | ||
var routeType = (_b = resource.route) === null || _b === void 0 ? void 0 : _b.type; | ||
if (baseUrlType === 'static' && routeType === 'static') { | ||
@@ -621,3 +609,3 @@ var stringsToJoin = [fetchBaseUrl, resourceRoute].filter(function (item) { return item; }).join('/'); | ||
}, true), | ||
], [types.identifier(String(fetchBaseUrl))]); | ||
], [types.identifier(fetchBaseUrl)]); | ||
} | ||
@@ -641,4 +629,4 @@ return types.templateLiteral(__spreadArray([ | ||
]), true), __spreadArray([ | ||
types.identifier(String(fetchBaseUrl)) | ||
], (routeType === 'static' ? [] : [types.identifier(String(resourceRoute))]), true)); | ||
types.identifier(fetchBaseUrl) | ||
], (routeType === 'static' ? [] : [types.identifier(resourceRoute)]), true)); | ||
}; | ||
@@ -650,20 +638,6 @@ var resolveResourceValue = function (value) { | ||
if (value.type === 'static') { | ||
return value.content; | ||
return value.value; | ||
} | ||
return "process.env.".concat(value.content); | ||
return "process.env.".concat(value.value) || value.fallback; | ||
}; | ||
var resolveObjectValue = function (prop) { | ||
if (prop.type === 'expr') { | ||
return types.identifier(prop.content); | ||
} | ||
var value = typeof prop.content === 'string' | ||
? types.stringLiteral(prop.content) | ||
: typeof prop.content === 'boolean' | ||
? types.booleanLiteral(prop.content) | ||
: typeof prop.content === 'number' | ||
? types.numericLiteral(prop.content) | ||
: types.identifier(String(prop.content)); | ||
return value; | ||
}; | ||
exports.resolveObjectValue = resolveObjectValue; | ||
//# sourceMappingURL=ast-utils.js.map |
@@ -177,51 +177,31 @@ var __assign = (this && this.__assign) || function () { | ||
var _a = node.content.nodes, success = _a.success, empty = _a.empty, error = _a.error, loading = _a.loading; | ||
var _b = node.content, loopItemsReference = _b.loopItemsReference, statePersistanceName = _b.statePersistanceName; | ||
var type = loopItemsReference.type; | ||
var errorNodeAST = null; | ||
var emptyNodeAST = null; | ||
var loadingNodeAST = null; | ||
var listNodeAST = null; | ||
var source = getRepeatSourceIdentifier(node.content.loopItemsReference, options); | ||
/* | ||
* TODO: @JK move this too into validator | ||
* CMS list node can only be a dynamic !! | ||
*/ | ||
if (type !== 'dynamic') { | ||
throw new Error("Node ".concat(node, " is dynamic, but the referece link is missing. \n\n Missing loopItemsReference")); | ||
} | ||
var statePersistanceName = node.content.statePersistanceName; | ||
var loadingStatePersistanceName = StringUtils.createStateOrPropStoringValue("".concat(statePersistanceName, "Loading")); | ||
var errorStatePersistanceName = StringUtils.createStateOrPropStoringValue("".concat(statePersistanceName, "Error")); | ||
if (empty) { | ||
emptyNodeAST = generateNode(empty, params, options)[0]; | ||
} | ||
if (error) { | ||
errorNodeAST = generateNode(error, params, options)[0]; | ||
params.stateDefinitions["".concat(statePersistanceName, "Error")] = { | ||
type: 'boolean', | ||
defaultValue: false, | ||
}; | ||
} | ||
if (loading) { | ||
loadingNodeAST = generateNode(loading, params, options)[0]; | ||
params.stateDefinitions["".concat(statePersistanceName, "Loading")] = { | ||
type: 'boolean', | ||
defaultValue: false, | ||
}; | ||
} | ||
if (success) { | ||
listNodeAST = generateNode(success, params, options)[0]; | ||
} | ||
var listNodeAST = !!success | ||
? generateNode(success, params, options)[0] | ||
: null; | ||
var source = getRepeatSourceIdentifier(node.content.loopItemsReference, options); | ||
var emptyNodeAST = !!empty | ||
? generateNode(empty, params, options)[0] | ||
: null; | ||
var emptyNodeExpressionAST = empty | ||
? types.logicalExpression('&&', types.unaryExpression('!', types.memberExpression(source, types.identifier('length'))), emptyNodeAST) | ||
: null; | ||
var errorNodeAST = !!error | ||
? generateNode(error, params, options)[0] | ||
: null; | ||
var errorNodeExpressionAST = error && errorStatePersistanceName | ||
? types.logicalExpression('&&', types.identifier(errorStatePersistanceName), errorNodeAST) | ||
: null; | ||
var loadingNodeAST = !!loading | ||
? generateNode(loading, params, options)[0] | ||
: null; | ||
var loadingNodeExpressionAST = loading && loadingStatePersistanceName | ||
? types.logicalExpression('&&', types.identifier(loadingStatePersistanceName), loadingNodeAST) | ||
: null; | ||
var _c = UIDLUtils.getRepeatIteratorNameAndKey({ | ||
var _b = UIDLUtils.getRepeatIteratorNameAndKey({ | ||
useIndex: true, | ||
iteratorName: 'item', | ||
}), iteratorName = _c.iteratorName, iteratorKey = _c.iteratorKey; | ||
}), iteratorName = _b.iteratorName, iteratorKey = _b.iteratorKey; | ||
var localIteratorPrefix = options.dynamicReferencePrefixMap.local; | ||
@@ -228,0 +208,0 @@ addDynamicAttributeToJSXTag(listNodeAST, 'key', iteratorKey, localIteratorPrefix); |
import * as types from '@babel/types'; | ||
import ParsedASTNode from './parsed-ast'; | ||
import { UIDLStateDefinition, UIDLPropDefinition, UIDLRawValue, UIDLStaticValue, UIDLDynamicReference, UIDLResourceItem, UIDLPropValue, UIDLExpressionValue } from '@teleporthq/teleport-types'; | ||
import { UIDLStateDefinition, UIDLPropDefinition, UIDLRawValue, Resource, UIDLDynamicReference } from '@teleporthq/teleport-types'; | ||
import { JSXGenerationOptions, JSXGenerationParams } from '../node-handlers/node-to-jsx/types'; | ||
@@ -58,7 +58,6 @@ /** | ||
export declare const wrapObjectPropertiesWithExpression: (properties: types.ObjectProperty[]) => types.ObjectExpression; | ||
export declare const generateRemoteResourceASTs: (resource: UIDLResourceItem) => types.VariableDeclaration[]; | ||
export declare const generateRemoteResourceASTs: (resource: Resource, propsPrefix?: string, extraUrlParamsGenerator?: () => types.ObjectProperty[]) => types.VariableDeclaration[]; | ||
export declare const generateMemberExpressionASTFromBase: (base: types.MemberExpression | types.Identifier, path: string[]) => types.MemberExpression; | ||
export declare const generateMemberExpressionASTFromPath: (path: Array<string | number>) => types.MemberExpression | types.Identifier; | ||
export declare const resolveObjectValue: (prop: UIDLStaticValue | UIDLPropValue | UIDLExpressionValue) => types.BooleanLiteral | types.Identifier | types.NumericLiteral | types.StringLiteral; | ||
export declare const generateMemberExpressionASTFromPath: (path: string[]) => types.MemberExpression | types.Identifier; | ||
export {}; | ||
//# sourceMappingURL=ast-utils.d.ts.map |
@@ -15,3 +15,2 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
import { createDynamicValueExpression } from '../node-handlers/node-to-jsx/utils'; | ||
import { ASTUtils } from '..'; | ||
/** | ||
@@ -77,6 +76,2 @@ * Adds a class definition string to an existing string of classes | ||
if (t === void 0) { t = types; } | ||
// TODO: Fix !! | ||
if (!value) { | ||
return; | ||
} | ||
var content = prefix === '' | ||
@@ -393,15 +388,10 @@ ? t.identifier(value) | ||
}; | ||
/* | ||
* TODO: Add the ability to support body and payload. | ||
* UIDLResourceItem['body'] | ||
* All the dynamic props are sent using the function props | ||
*/ | ||
export var generateRemoteResourceASTs = function (resource) { | ||
export var generateRemoteResourceASTs = function (resource, propsPrefix, extraUrlParamsGenerator) { | ||
if (propsPrefix === void 0) { propsPrefix = ''; } | ||
var fetchUrl = computeFetchUrl(resource); | ||
var authHeaderAST = computeAuthorizationHeaderAST(resource === null || resource === void 0 ? void 0 : resource.headers); | ||
var headersASTs = generateRESTHeadersAST(resource === null || resource === void 0 ? void 0 : resource.headers); | ||
var queryParams = generateURLParamsAST(resource === null || resource === void 0 ? void 0 : resource.params); | ||
var authHeaderAST = computeAuthorizationHeaderAST(resource); | ||
var queryParams = generateURLParamsAST(resource.urlParams, propsPrefix, extraUrlParamsGenerator); | ||
var fetchUrlQuasis = fetchUrl.quasis; | ||
var queryParamsQuasis = (queryParams === null || queryParams === void 0 ? void 0 : queryParams.quasis) || [types.templateElement({ raw: '', cooked: '' })]; | ||
if ((queryParams === null || queryParams === void 0 ? void 0 : queryParams.expressions.length) > 0) { | ||
var queryParamsQuasis = queryParams.quasis; | ||
if (queryParams.expressions.length > 0) { | ||
fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw = | ||
@@ -413,18 +403,3 @@ fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw + '?'; | ||
} | ||
var paramsDeclerations = Object.keys((resource === null || resource === void 0 ? void 0 : resource.params) || {}).reduce(function (acc, item) { | ||
var prop = resource.params[item]; | ||
if (prop.type === 'static') { | ||
acc.push(types.objectProperty(types.stringLiteral(item), ASTUtils.resolveObjectValue(prop))); | ||
} | ||
if (prop.type === 'dynamic') { | ||
acc.push(types.spreadElement(types.logicalExpression('&&', types.memberExpression(types.identifier('params'), types.stringLiteral(item), true, false), types.objectExpression([ | ||
types.objectProperty(types.stringLiteral(item), types.memberExpression(types.identifier('params'), types.stringLiteral(item), true, false)), | ||
])))); | ||
} | ||
return acc; | ||
}, []); | ||
var paramsAST = types.variableDeclaration('const', [ | ||
types.variableDeclarator(types.identifier('urlParams'), types.objectExpression(paramsDeclerations)), | ||
]); | ||
var url = (queryParams === null || queryParams === void 0 ? void 0 : queryParams.quasis) | ||
var url = queryParams | ||
? types.templateLiteral(__spreadArray(__spreadArray([], fetchUrlQuasis, true), queryParamsQuasis, true), __spreadArray([], fetchUrl.expressions.concat(queryParams.expressions), true)) | ||
@@ -436,4 +411,6 @@ : fetchUrl; | ||
types.objectExpression([ | ||
types.objectProperty(types.identifier('method'), types.stringLiteral(resource.method)), | ||
types.objectProperty(types.identifier('headers'), types.objectExpression(__spreadArray(__spreadArray([], headersASTs, true), [authHeaderAST], false))), | ||
types.objectProperty(types.identifier('headers'), types.objectExpression([ | ||
types.objectProperty(types.identifier('"Content-Type"'), types.stringLiteral('application/json'), false, false), | ||
authHeaderAST, | ||
])), | ||
]), | ||
@@ -445,11 +422,4 @@ ]))), | ||
]); | ||
return [paramsAST, fetchAST, responseJSONAST]; | ||
return [fetchAST, responseJSONAST]; | ||
}; | ||
var generateRESTHeadersAST = function (headers) { | ||
return Object.keys(headers) | ||
.filter(function (header) { return header !== 'authToken'; }) | ||
.map(function (header) { | ||
return types.objectProperty(types.stringLiteral(header), types.stringLiteral(String(headers[header].content))); | ||
}); | ||
}; | ||
export var generateMemberExpressionASTFromBase = function (base, path) { | ||
@@ -466,43 +436,62 @@ if (path.length === 1) { | ||
if (path.length === 1) { | ||
return types.identifier(path[0].toString()); | ||
return types.identifier(path[0]); | ||
} | ||
pathClone.pop(); | ||
var currentPath = path[path.length - 1]; | ||
if (typeof currentPath === 'number') { | ||
return types.memberExpression(generateMemberExpressionASTFromPath(pathClone), types.numericLiteral(currentPath), true); | ||
} | ||
var containsSpecial = currentPath.indexOf('.') !== -1 || currentPath.indexOf('-') !== -1; | ||
return types.memberExpression(generateMemberExpressionASTFromPath(pathClone), containsSpecial ? types.stringLiteral(currentPath) : types.identifier(currentPath), containsSpecial); | ||
return types.memberExpression(generateMemberExpressionASTFromPath(pathClone), types.identifier(path[path.length - 1]), false); | ||
}; | ||
var generateURLParamsAST = function (urlParams) { | ||
if (!urlParams) { | ||
return null; | ||
} | ||
var generateURLParamsAST = function (urlParams, propsPrefix, extraUrlParamsGenerator) { | ||
var queryString = {}; | ||
Object.keys(urlParams).forEach(function (key) { | ||
resolveDynamicValuesFromUrlParams(urlParams[key], queryString, key); | ||
resolveDynamicValuesFromUrlParams(urlParams[key], queryString, key, propsPrefix); | ||
}); | ||
var urlObject = types.objectExpression(__spreadArray(__spreadArray([], Object.keys(queryString).map(function (key) { | ||
return types.objectProperty(types.stringLiteral("".concat(key)), queryString[key]); | ||
}), true), (extraUrlParamsGenerator ? extraUrlParamsGenerator() : []), true)); | ||
return types.templateLiteral([ | ||
types.templateElement({ raw: '', cooked: '' }, false), | ||
types.templateElement({ raw: '', cooked: '' }, true), | ||
], [types.newExpression(types.identifier('URLSearchParams'), [types.identifier('urlParams')])]); | ||
], [types.newExpression(types.identifier('URLSearchParams'), [urlObject])]); | ||
}; | ||
var resolveDynamicValuesFromUrlParams = function (field, query, prefix) { | ||
var resolveDynamicValuesFromUrlParams = function (field, query, prefix, propsPrefix) { | ||
if (prefix === void 0) { prefix = null; } | ||
if (field.type === 'dynamic' || field.type === 'static') { | ||
query[prefix] = resolveUrlParamsValue(field); | ||
if (propsPrefix === void 0) { propsPrefix = ''; } | ||
if (Array.isArray(field)) { | ||
var arrayValues = field.map(function (value) { | ||
return resolveUrlParamsValue(value, propsPrefix); | ||
}); | ||
query[prefix] = types.arrayExpression(arrayValues); | ||
return; | ||
} | ||
if (field.type === 'dynamic' || field.type === 'static' || field.type === 'expr') { | ||
query[prefix] = resolveUrlParamsValue(field, propsPrefix); | ||
return; | ||
} | ||
Object.keys(field).forEach(function (key) { | ||
var value = field[key]; | ||
var newPrefix = prefix ? "".concat(prefix, "[").concat(key, "]") : key; | ||
if (typeof value === 'object') { | ||
resolveDynamicValuesFromUrlParams(value, query, newPrefix, propsPrefix); | ||
return; | ||
} | ||
query[newPrefix] = resolveUrlParamsValue(value, propsPrefix); | ||
}); | ||
}; | ||
var resolveUrlParamsValue = function (urlParam) { | ||
if (urlParam.type === 'static') { | ||
return types.stringLiteral("".concat(urlParam.content)); | ||
var resolveUrlParamsValue = function (urlParams, propsPrefix) { | ||
if (propsPrefix === void 0) { propsPrefix = ''; } | ||
if (urlParams.type === 'static') { | ||
return types.stringLiteral("".concat(urlParams.content)); | ||
} | ||
if (urlParam.content.referenceType !== 'prop') { | ||
if (urlParams.type === 'expr') { | ||
return types.identifier(urlParams.content); | ||
} | ||
if (urlParams.content.referenceType !== 'prop') { | ||
throw new Error('Only prop references are supported for url params'); | ||
} | ||
var paramPath = ['params', urlParam.content.id]; | ||
var paramPath = __spreadArray(__spreadArray([], (propsPrefix ? [propsPrefix] : []), true), (urlParams.content.path || []), true); | ||
var templateLiteralElements = paramPath | ||
.map(function (_, index) { | ||
var isTail = index === paramPath.length - 1; | ||
if (index === paramPath.length - 1) { | ||
return null; | ||
} | ||
var isTail = index === paramPath.length - 2; | ||
return types.templateElement({ | ||
@@ -518,9 +507,9 @@ cooked: '', | ||
}; | ||
var computeAuthorizationHeaderAST = function (headers) { | ||
var computeAuthorizationHeaderAST = function (resource) { | ||
var _a; | ||
var authToken = resolveResourceValue(headers.authToken); | ||
var authToken = resolveResourceValue(resource.authToken); | ||
if (!authToken) { | ||
return null; | ||
} | ||
var authTokenType = (_a = headers.authToken) === null || _a === void 0 ? void 0 : _a.type; | ||
var authTokenType = (_a = resource.authToken) === null || _a === void 0 ? void 0 : _a.type; | ||
return types.objectProperty(types.identifier('Authorization'), types.templateLiteral(__spreadArray([ | ||
@@ -538,11 +527,10 @@ types.templateElement({ | ||
}, true), | ||
]), true), __spreadArray([], (authTokenType === 'static' ? [] : [types.identifier(String(authToken))]), true)), false, false); | ||
]), true), __spreadArray([], (authTokenType === 'static' ? [] : [types.identifier(authToken)]), true)), false, false); | ||
}; | ||
var computeFetchUrl = function (resource) { | ||
var _a, _b; | ||
var path = resource.path; | ||
var fetchBaseUrl = resolveResourceValue(path.baseUrl); | ||
var resourceRoute = resolveResourceValue(path.route); | ||
var baseUrlType = (_a = path.baseUrl) === null || _a === void 0 ? void 0 : _a.type; | ||
var routeType = (_b = path.route) === null || _b === void 0 ? void 0 : _b.type; | ||
var fetchBaseUrl = resolveResourceValue(resource.baseUrl); | ||
var resourceRoute = resolveResourceValue(resource.route); | ||
var baseUrlType = (_a = resource.baseUrl) === null || _a === void 0 ? void 0 : _a.type; | ||
var routeType = (_b = resource.route) === null || _b === void 0 ? void 0 : _b.type; | ||
if (baseUrlType === 'static' && routeType === 'static') { | ||
@@ -564,3 +552,3 @@ var stringsToJoin = [fetchBaseUrl, resourceRoute].filter(function (item) { return item; }).join('/'); | ||
}, true), | ||
], [types.identifier(String(fetchBaseUrl))]); | ||
], [types.identifier(fetchBaseUrl)]); | ||
} | ||
@@ -584,4 +572,4 @@ return types.templateLiteral(__spreadArray([ | ||
]), true), __spreadArray([ | ||
types.identifier(String(fetchBaseUrl)) | ||
], (routeType === 'static' ? [] : [types.identifier(String(resourceRoute))]), true)); | ||
types.identifier(fetchBaseUrl) | ||
], (routeType === 'static' ? [] : [types.identifier(resourceRoute)]), true)); | ||
}; | ||
@@ -593,19 +581,6 @@ var resolveResourceValue = function (value) { | ||
if (value.type === 'static') { | ||
return value.content; | ||
return value.value; | ||
} | ||
return "process.env.".concat(value.content); | ||
return "process.env.".concat(value.value) || value.fallback; | ||
}; | ||
export var resolveObjectValue = function (prop) { | ||
if (prop.type === 'expr') { | ||
return types.identifier(prop.content); | ||
} | ||
var value = typeof prop.content === 'string' | ||
? types.stringLiteral(prop.content) | ||
: typeof prop.content === 'boolean' | ||
? types.booleanLiteral(prop.content) | ||
: typeof prop.content === 'number' | ||
? types.numericLiteral(prop.content) | ||
: types.identifier(String(prop.content)); | ||
return value; | ||
}; | ||
//# sourceMappingURL=ast-utils.js.map |
{ | ||
"name": "@teleporthq/teleport-plugin-common", | ||
"version": "0.27.3-alpha.4", | ||
"version": "0.27.3-alpha.5", | ||
"description": "Common building and modelating functions for ASTs and HASTs", | ||
@@ -28,8 +28,8 @@ "author": "teleportHQ", | ||
"@babel/types": "^7.5.5", | ||
"@teleporthq/teleport-shared": "^0.27.3-alpha.4", | ||
"@teleporthq/teleport-types": "^0.27.3-alpha.4", | ||
"@teleporthq/teleport-shared": "^0.27.3-alpha.5", | ||
"@teleporthq/teleport-types": "^0.27.3-alpha.2", | ||
"jss": "^10.0.0", | ||
"jss-preset-default": "^10.0.0" | ||
}, | ||
"gitHead": "aef4f43968d22682591f84d9080e3c5844318f9a" | ||
"gitHead": "c7a1ae9cf5878d2390df5dbeb2db5349d16744c3" | ||
} |
@@ -261,20 +261,3 @@ import * as types from '@babel/types' | ||
const { success, empty, error, loading } = node.content.nodes | ||
const { loopItemsReference, statePersistanceName } = node.content | ||
const { type } = loopItemsReference | ||
let errorNodeAST: types.JSXElement | null = null | ||
let emptyNodeAST: types.JSXElement | null = null | ||
let loadingNodeAST: types.JSXElement | null = null | ||
let listNodeAST: types.JSXElement | null = null | ||
const source = getRepeatSourceIdentifier(node.content.loopItemsReference, options) | ||
/* | ||
* TODO: @JK move this too into validator | ||
* CMS list node can only be a dynamic !! | ||
*/ | ||
if (type !== 'dynamic') { | ||
throw new Error(`Node ${node} is dynamic, but the referece link is missing. \n | ||
Missing loopItemsReference`) | ||
} | ||
const { statePersistanceName } = node.content | ||
const loadingStatePersistanceName = StringUtils.createStateOrPropStoringValue( | ||
@@ -287,26 +270,10 @@ `${statePersistanceName}Loading` | ||
if (empty) { | ||
emptyNodeAST = generateNode(empty, params, options)[0] as types.JSXElement | ||
} | ||
const listNodeAST = !!success | ||
? (generateNode(success, params, options) as types.JSXElement[])[0] | ||
: null | ||
const source = getRepeatSourceIdentifier(node.content.loopItemsReference, options) | ||
if (error) { | ||
errorNodeAST = generateNode(error, params, options)[0] as types.JSXElement | ||
params.stateDefinitions[`${statePersistanceName}Error`] = { | ||
type: 'boolean', | ||
defaultValue: false, | ||
} | ||
} | ||
if (loading) { | ||
loadingNodeAST = generateNode(loading, params, options)[0] as types.JSXElement | ||
params.stateDefinitions[`${statePersistanceName}Loading`] = { | ||
type: 'boolean', | ||
defaultValue: false, | ||
} | ||
} | ||
if (success) { | ||
listNodeAST = (generateNode(success, params, options) as types.JSXElement[])[0] | ||
} | ||
const emptyNodeAST = !!empty | ||
? (generateNode(empty, params, options)[0] as types.JSXElement) | ||
: null | ||
const emptyNodeExpressionAST = empty | ||
@@ -320,2 +287,5 @@ ? types.logicalExpression( | ||
const errorNodeAST = !!error | ||
? (generateNode(error, params, options)[0] as types.JSXElement) | ||
: null | ||
const errorNodeExpressionAST = | ||
@@ -326,2 +296,5 @@ error && errorStatePersistanceName | ||
const loadingNodeAST = !!loading | ||
? (generateNode(loading, params, options)[0] as types.JSXElement) | ||
: null | ||
const loadingNodeExpressionAST = | ||
@@ -328,0 +301,0 @@ loading && loadingStatePersistanceName |
@@ -9,7 +9,8 @@ import * as types from '@babel/types' | ||
UIDLRawValue, | ||
Resource, | ||
ResourceValue, | ||
ResourceUrlParams, | ||
UIDLStaticValue, | ||
UIDLDynamicReference, | ||
UIDLResourceItem, | ||
UIDLENVValue, | ||
UIDLPropValue, | ||
ResourceUrlValues, | ||
UIDLExpressionValue, | ||
@@ -19,4 +20,2 @@ } from '@teleporthq/teleport-types' | ||
import { createDynamicValueExpression } from '../node-handlers/node-to-jsx/utils' | ||
import { ASTUtils } from '..' | ||
/** | ||
@@ -109,7 +108,2 @@ * Adds a class definition string to an existing string of classes | ||
) => { | ||
// TODO: Fix !! | ||
if (!value) { | ||
return | ||
} | ||
const content = | ||
@@ -606,17 +600,20 @@ prefix === '' | ||
/* | ||
* TODO: Add the ability to support body and payload. | ||
* UIDLResourceItem['body'] | ||
* All the dynamic props are sent using the function props | ||
*/ | ||
export const generateRemoteResourceASTs = (resource: UIDLResourceItem) => { | ||
export const generateRemoteResourceASTs = ( | ||
resource: Resource, | ||
propsPrefix: string = '', | ||
extraUrlParamsGenerator?: () => types.ObjectProperty[] | ||
) => { | ||
const fetchUrl = computeFetchUrl(resource) | ||
const authHeaderAST = computeAuthorizationHeaderAST(resource?.headers) | ||
const headersASTs = generateRESTHeadersAST(resource?.headers) | ||
const authHeaderAST = computeAuthorizationHeaderAST(resource) | ||
const queryParams = generateURLParamsAST(resource?.params) | ||
const queryParams = generateURLParamsAST( | ||
resource.urlParams as ResourceUrlParams, | ||
propsPrefix, | ||
extraUrlParamsGenerator | ||
) | ||
const fetchUrlQuasis = fetchUrl.quasis | ||
const queryParamsQuasis = queryParams?.quasis || [types.templateElement({ raw: '', cooked: '' })] | ||
const queryParamsQuasis = queryParams.quasis | ||
if (queryParams?.expressions.length > 0) { | ||
if (queryParams.expressions.length > 0) { | ||
fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw = | ||
@@ -631,48 +628,3 @@ fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw + '?' | ||
const paramsDeclerations: Array<types.ObjectProperty | types.SpreadElement> = Object.keys( | ||
resource?.params || {} | ||
).reduce((acc: Array<types.ObjectProperty | types.SpreadElement>, item) => { | ||
const prop = resource.params[item] | ||
if (prop.type === 'static') { | ||
acc.push(types.objectProperty(types.stringLiteral(item), ASTUtils.resolveObjectValue(prop))) | ||
} | ||
if (prop.type === 'dynamic') { | ||
acc.push( | ||
types.spreadElement( | ||
types.logicalExpression( | ||
'&&', | ||
types.memberExpression( | ||
types.identifier('params'), | ||
types.stringLiteral(item), | ||
true, | ||
false | ||
), | ||
types.objectExpression([ | ||
types.objectProperty( | ||
types.stringLiteral(item), | ||
types.memberExpression( | ||
types.identifier('params'), | ||
types.stringLiteral(item), | ||
true, | ||
false | ||
) | ||
), | ||
]) | ||
) | ||
) | ||
) | ||
} | ||
return acc | ||
}, []) | ||
const paramsAST = types.variableDeclaration('const', [ | ||
types.variableDeclarator( | ||
types.identifier('urlParams'), | ||
types.objectExpression(paramsDeclerations) | ||
), | ||
]) | ||
const url = queryParams?.quasis | ||
const url = queryParams | ||
? types.templateLiteral( | ||
@@ -691,6 +643,13 @@ [...fetchUrlQuasis, ...queryParamsQuasis], | ||
types.objectExpression([ | ||
types.objectProperty(types.identifier('method'), types.stringLiteral(resource.method)), | ||
types.objectProperty( | ||
types.identifier('headers'), | ||
types.objectExpression([...headersASTs, authHeaderAST]) | ||
types.objectExpression([ | ||
types.objectProperty( | ||
types.identifier('"Content-Type"'), | ||
types.stringLiteral('application/json'), | ||
false, | ||
false | ||
), | ||
authHeaderAST, | ||
]) | ||
), | ||
@@ -715,16 +674,5 @@ ]), | ||
return [paramsAST, fetchAST, responseJSONAST] | ||
return [fetchAST, responseJSONAST] | ||
} | ||
const generateRESTHeadersAST = (headers: UIDLResourceItem['headers']): types.ObjectProperty[] => { | ||
return Object.keys(headers) | ||
.filter((header) => header !== 'authToken') | ||
.map((header) => { | ||
return types.objectProperty( | ||
types.stringLiteral(header), | ||
types.stringLiteral(String(headers[header].content)) | ||
) | ||
}) | ||
} | ||
export const generateMemberExpressionASTFromBase = ( | ||
@@ -749,7 +697,7 @@ base: types.MemberExpression | types.Identifier, | ||
export const generateMemberExpressionASTFromPath = ( | ||
path: Array<string | number> | ||
path: string[] | ||
): types.MemberExpression | types.Identifier => { | ||
const pathClone = [...path] | ||
if (path.length === 1) { | ||
return types.identifier(path[0].toString()) | ||
return types.identifier(path[0]) | ||
} | ||
@@ -759,17 +707,6 @@ | ||
const currentPath = path[path.length - 1] | ||
if (typeof currentPath === 'number') { | ||
return types.memberExpression( | ||
generateMemberExpressionASTFromPath(pathClone), | ||
types.numericLiteral(currentPath), | ||
true | ||
) | ||
} | ||
const containsSpecial = currentPath.indexOf('.') !== -1 || currentPath.indexOf('-') !== -1 | ||
return types.memberExpression( | ||
generateMemberExpressionASTFromPath(pathClone), | ||
containsSpecial ? types.stringLiteral(currentPath) : types.identifier(currentPath), | ||
containsSpecial | ||
types.identifier(path[path.length - 1]), | ||
false | ||
) | ||
@@ -779,13 +716,18 @@ } | ||
const generateURLParamsAST = ( | ||
urlParams: Record<string, UIDLStaticValue | UIDLPropValue> | ||
): types.TemplateLiteral | null => { | ||
if (!urlParams) { | ||
return null | ||
} | ||
urlParams: ResourceUrlParams, | ||
propsPrefix?: string, | ||
extraUrlParamsGenerator?: () => types.ObjectProperty[] | ||
) => { | ||
const queryString: Record<string, types.Expression> = {} | ||
Object.keys(urlParams).forEach((key) => { | ||
resolveDynamicValuesFromUrlParams(urlParams[key], queryString, key) | ||
resolveDynamicValuesFromUrlParams(urlParams[key], queryString, key, propsPrefix) | ||
}) | ||
const urlObject = types.objectExpression([ | ||
...Object.keys(queryString).map((key) => { | ||
return types.objectProperty(types.stringLiteral(`${key}`), queryString[key]) | ||
}), | ||
...(extraUrlParamsGenerator ? extraUrlParamsGenerator() : []), | ||
]) | ||
return types.templateLiteral( | ||
@@ -796,3 +738,3 @@ [ | ||
], | ||
[types.newExpression(types.identifier('URLSearchParams'), [types.identifier('urlParams')])] | ||
[types.newExpression(types.identifier('URLSearchParams'), [urlObject])] | ||
) | ||
@@ -802,25 +744,57 @@ } | ||
const resolveDynamicValuesFromUrlParams = ( | ||
field: UIDLStaticValue | UIDLPropValue, | ||
field: ResourceUrlValues, | ||
query: Record<string, types.Expression>, | ||
prefix: string = null | ||
prefix: string = null, | ||
propsPrefix: string = '' | ||
) => { | ||
if (field.type === 'dynamic' || field.type === 'static') { | ||
query[prefix] = resolveUrlParamsValue(field) | ||
if (Array.isArray(field)) { | ||
const arrayValues = field.map((value) => { | ||
return resolveUrlParamsValue(value, propsPrefix) | ||
}) | ||
query[prefix] = types.arrayExpression(arrayValues) | ||
return | ||
} | ||
if (field.type === 'dynamic' || field.type === 'static' || field.type === 'expr') { | ||
query[prefix] = resolveUrlParamsValue(field, propsPrefix) | ||
return | ||
} | ||
Object.keys(field).forEach((key) => { | ||
const value = field[key] | ||
const newPrefix = prefix ? `${prefix}[${key}]` : key | ||
if (typeof value === 'object') { | ||
resolveDynamicValuesFromUrlParams(value, query, newPrefix, propsPrefix) | ||
return | ||
} | ||
query[newPrefix] = resolveUrlParamsValue(value, propsPrefix) | ||
}) | ||
} | ||
const resolveUrlParamsValue = (urlParam: UIDLStaticValue | UIDLPropValue) => { | ||
if (urlParam.type === 'static') { | ||
return types.stringLiteral(`${urlParam.content}`) | ||
const resolveUrlParamsValue = ( | ||
urlParams: UIDLStaticValue | UIDLDynamicReference | UIDLExpressionValue, | ||
propsPrefix: string = '' | ||
) => { | ||
if (urlParams.type === 'static') { | ||
return types.stringLiteral(`${urlParams.content}`) | ||
} | ||
if (urlParam.content.referenceType !== 'prop') { | ||
if (urlParams.type === 'expr') { | ||
return types.identifier(urlParams.content) | ||
} | ||
if (urlParams.content.referenceType !== 'prop') { | ||
throw new Error('Only prop references are supported for url params') | ||
} | ||
const paramPath = ['params', urlParam.content.id] | ||
const paramPath = [...(propsPrefix ? [propsPrefix] : []), ...(urlParams.content.path || [])] | ||
const templateLiteralElements = paramPath | ||
.map((_, index) => { | ||
const isTail = index === paramPath.length - 1 | ||
if (index === paramPath.length - 1) { | ||
return null | ||
} | ||
const isTail = index === paramPath.length - 2 | ||
return types.templateElement( | ||
@@ -841,4 +815,4 @@ { | ||
const computeAuthorizationHeaderAST = (headers: UIDLResourceItem['headers']) => { | ||
const authToken = resolveResourceValue(headers.authToken) | ||
const computeAuthorizationHeaderAST = (resource: Resource) => { | ||
const authToken = resolveResourceValue(resource.authToken) | ||
if (!authToken) { | ||
@@ -848,3 +822,3 @@ return null | ||
const authTokenType = headers.authToken?.type | ||
const authTokenType = resource.authToken?.type | ||
@@ -874,3 +848,3 @@ return types.objectProperty( | ||
], | ||
[...(authTokenType === 'static' ? [] : [types.identifier(String(authToken))])] | ||
[...(authTokenType === 'static' ? [] : [types.identifier(authToken)])] | ||
), | ||
@@ -882,9 +856,8 @@ false, | ||
const computeFetchUrl = (resource: UIDLResourceItem) => { | ||
const { path } = resource | ||
const fetchBaseUrl = resolveResourceValue(path.baseUrl) | ||
const resourceRoute = resolveResourceValue(path.route) | ||
const computeFetchUrl = (resource: Resource) => { | ||
const fetchBaseUrl = resolveResourceValue(resource.baseUrl) | ||
const resourceRoute = resolveResourceValue(resource.route) | ||
const baseUrlType = path.baseUrl?.type | ||
const routeType = path.route?.type | ||
const baseUrlType = resource.baseUrl?.type | ||
const routeType = resource.route?.type | ||
@@ -922,3 +895,3 @@ if (baseUrlType === 'static' && routeType === 'static') { | ||
], | ||
[types.identifier(String(fetchBaseUrl))] | ||
[types.identifier(fetchBaseUrl)] | ||
) | ||
@@ -956,4 +929,4 @@ } | ||
[ | ||
types.identifier(String(fetchBaseUrl)), | ||
...(routeType === 'static' ? [] : [types.identifier(String(resourceRoute))]), | ||
types.identifier(fetchBaseUrl), | ||
...(routeType === 'static' ? [] : [types.identifier(resourceRoute)]), | ||
] | ||
@@ -963,3 +936,3 @@ ) | ||
const resolveResourceValue = (value: UIDLStaticValue | UIDLENVValue) => { | ||
const resolveResourceValue = (value: ResourceValue) => { | ||
if (!value) { | ||
@@ -970,23 +943,6 @@ return '' | ||
if (value.type === 'static') { | ||
return value.content | ||
return value.value | ||
} | ||
return `process.env.${value.content}` | ||
return `process.env.${value.value}` || value.fallback | ||
} | ||
export const resolveObjectValue = (prop: UIDLStaticValue | UIDLPropValue | UIDLExpressionValue) => { | ||
if (prop.type === 'expr') { | ||
return types.identifier(prop.content) | ||
} | ||
const value = | ||
typeof prop.content === 'string' | ||
? types.stringLiteral(prop.content) | ||
: typeof prop.content === 'boolean' | ||
? types.booleanLiteral(prop.content) | ||
: typeof prop.content === 'number' | ||
? types.numericLiteral(prop.content) | ||
: types.identifier(String(prop.content)) | ||
return value | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
568762
-1.91%7697
-1.95%+ Added
+ Added
- Removed
- Removed