Skip to content

Commit

Permalink
Revert "Add support for wordpress - minor changes required (#790)" (#792
Browse files Browse the repository at this point in the history
)

This reverts commit fefe05d.
  • Loading branch information
JayaKrishnaNamburu committed May 22, 2023
1 parent fefe05d commit 1ec82a1
Show file tree
Hide file tree
Showing 23 changed files with 2,391 additions and 4,359 deletions.
3,870 changes: 1,738 additions & 2,132 deletions examples/uidl-samples/cms-project.json

Large diffs are not rendered by default.

1,257 changes: 94 additions & 1,163 deletions examples/uidl-samples/component.json

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
"add-contributor": "all-contributors add",
"changelog": "lerna-changelog",
"version": "lerna version",
"many:check": "manypkg check",
"many:fix": "manypkg fix",
"publish": "lerna publish from-package --no-verify-access",
"local:adduser": "npm adduser --registry http://localhost:4873",
"local:publish": "lerna exec --bail=false -- npm publish -f --registry http://localhost:4873",
"local:unpublish": "lerna exec --bail=false -- npm unpublish -f --registry http://localhost:4873"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged && npm run lint && npm run many:check"
"pre-commit": "pretty-quick --staged && npm run lint"
}
},
"engines": {
Expand All @@ -43,8 +41,7 @@
"author": "teleportHQ",
"license": "MIT",
"homepage": "https://teleporthq.io/",
"dependencies": {
"@manypkg/cli": "^0.20.0",
"devDependencies": {
"@types/babel-types": "^7.0.7",
"@types/jest": "^25.1.1",
"@types/node": "^16",
Expand All @@ -60,7 +57,7 @@
"jest": "^26.6.3",
"lerna": "^5.3.0",
"lerna-changelog": "^2.2.0",
"prettier": "^2.3.1",
"prettier": "^2.8.8",
"pretty-quick": "^3.1.0",
"rimraf": "^3.0.0",
"ts-jest": "^26.5.6",
Expand Down
7 changes: 7 additions & 0 deletions packages/teleport-code-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import {
PlainHTMLMapping,
} from '@teleporthq/teleport-component-generator-html'
import { isNodeProcess } from './utils'
import { ProjectPluginContexts } from '@teleporthq/teleport-project-plugin-contexts'
import { ProjectPluginApiRoutes } from '@teleporthq/teleport-project-plugin-api-routes'

const componentGeneratorFactories: Record<ComponentType, ComponentGeneratorInstance> = {
[ComponentType.REACT]: createReactComponentGenerator,
Expand Down Expand Up @@ -144,6 +146,11 @@ export const packProject: PackProjectFunction = async (
projectGeneratorFactory.addPlugin(htmlErrorPageMapping)
}

if (projectType === ProjectType.NEXT) {
projectGeneratorFactory.addPlugin(new ProjectPluginContexts({ framework: ProjectType.NEXT }))
projectGeneratorFactory.addPlugin(new ProjectPluginApiRoutes({ framework: ProjectType.NEXT }))
}

if (projectType === ProjectType.NUXT) {
projectGeneratorFactory.addPlugin(nuxtErrorPageMapper)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ComponentPlugin,
ComponentUIDL,
GeneratorOptions,
UIDLRootComponent,
} from '@teleporthq/teleport-types'
import { UIDLUtils } from '@teleporthq/teleport-shared'

Expand All @@ -16,7 +15,7 @@ export default class AssemblyLine {
}

public async run(
uidl: ComponentUIDL | UIDLRootComponent,
uidl: ComponentUIDL,
options: GeneratorOptions,
initialStructure: ComponentStructure = {
uidl,
Expand Down
172 changes: 94 additions & 78 deletions packages/teleport-plugin-common/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
UIDLStateDefinition,
UIDLPropDefinition,
UIDLRawValue,
Resource,
ResourceValue,
ResourceUrlParams,
UIDLStaticValue,
UIDLDynamicReference,
UIDLResourceItem,
UIDLENVValue,
UIDLDynamicFunctionParam,
ResourceUrlValues,
UIDLExpressionValue,
} from '@teleporthq/teleport-types'
import { JSXGenerationOptions, JSXGenerationParams } from '../node-handlers/node-to-jsx/types'
import { createDynamicValueExpression } from '../node-handlers/node-to-jsx/utils'
Expand Down Expand Up @@ -161,6 +163,7 @@ export const addDynamicExpressionAttributeToJSXTag = (
const { projectContexts } = params
const contextMeta = projectContexts[entityToResolve.content.ctxId]
const nameOfContext = StringUtils.camelize(contextMeta.providerName)

return nameOfContext
}
return name
Expand Down Expand Up @@ -565,8 +568,8 @@ export const createStateHookAST = (
return t.variableDeclaration('const', [
t.variableDeclarator(
t.arrayPattern([
t.identifier(stateKey),
t.identifier(`set${StringUtils.capitalize(stateKey)}`),
t.identifier(StringUtils.createStateOrPropStoringValue(stateKey)),
t.identifier(StringUtils.createStateStoringFunction(stateKey)),
]),
t.callExpression(t.identifier('useState'), [defaultValueArgument])
),
Expand All @@ -591,24 +594,24 @@ export const generateDynamicWindowImport = (
export const wrapObjectPropertiesWithExpression = (properties: types.ObjectProperty[]) =>
types.objectExpression(properties)

/*
* 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,
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.urlParams as ResourceUrlParams,
propsPrefix,
extraUrlParamsGenerator
)

const queryParams = generateURLParamsAST(resource?.params, 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 =
fetchUrlQuasis[fetchUrlQuasis.length - 1].value.raw + '?'

Expand All @@ -618,7 +621,7 @@ export const generateRemoteResourceASTs = (
queryParamsQuasis.pop()
}

const url = queryParams?.quasis
const url = queryParams
? types.templateLiteral(
[...fetchUrlQuasis, ...queryParamsQuasis],
[...fetchUrl.expressions.concat(queryParams.expressions)]
Expand All @@ -632,10 +635,17 @@ export const generateRemoteResourceASTs = (
types.callExpression(types.identifier('fetch'), [
url,
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,
])
),
]),
])
Expand All @@ -658,17 +668,6 @@ export const generateRemoteResourceASTs = (
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 = (
base: types.MemberExpression | types.Identifier,
path: string[]
Expand All @@ -688,44 +687,30 @@ export const generateMemberExpressionASTFromBase = (
}

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])
}

pathClone.pop()

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
)
}

const generateURLParamsAST = (
urlParams: Record<string, UIDLStaticValue | UIDLDynamicFunctionParam>,
urlParams: ResourceUrlParams,
propsPrefix?: string,
extraUrlParamsGenerator?: () => types.ObjectProperty[]
): types.TemplateLiteral | null => {
if (!urlParams) {
return null
}

) => {
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([
Expand All @@ -745,29 +730,61 @@ const generateURLParamsAST = (
}

const resolveDynamicValuesFromUrlParams = (
field: UIDLStaticValue | UIDLDynamicFunctionParam,
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 | UIDLDynamicFunctionParam) => {
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 (urlParams.type === 'expr') {
return types.identifier(urlParams.content)
}

if (urlParam.content.referenceType !== 'prop') {
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(
{
cooked: '',
Expand All @@ -783,13 +800,13 @@ const resolveUrlParamsValue = (urlParam: UIDLStaticValue | UIDLDynamicFunctionPa
])
}

const computeAuthorizationHeaderAST = (headers: UIDLResourceItem['headers']) => {
const authToken = resolveResourceValue(headers.authToken)
const computeAuthorizationHeaderAST = (resource: Resource) => {
const authToken = resolveResourceValue(resource.authToken)
if (!authToken) {
return null
}

const authTokenType = headers.authToken?.type
const authTokenType = resource.authToken?.type

return types.objectProperty(
types.identifier('Authorization'),
Expand All @@ -814,20 +831,19 @@ const computeAuthorizationHeaderAST = (headers: UIDLResourceItem['headers']) =>
),
]),
],
[...(authTokenType === 'static' ? [] : [types.identifier(String(authToken))])]
[...(authTokenType === 'static' ? [] : [types.identifier(authToken)])]
),
false,
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

if (baseUrlType === 'static' && routeType === 'static') {
const stringsToJoin = [fetchBaseUrl, resourceRoute].filter((item) => item).join('/')
Expand Down Expand Up @@ -860,7 +876,7 @@ const computeFetchUrl = (resource: UIDLResourceItem) => {
true
),
],
[types.identifier(String(fetchBaseUrl))]
[types.identifier(fetchBaseUrl)]
)
}

Expand Down Expand Up @@ -893,20 +909,20 @@ const computeFetchUrl = (resource: UIDLResourceItem) => {
]),
],
[
types.identifier(String(fetchBaseUrl)),
...(routeType === 'static' ? [] : [types.identifier(String(resourceRoute))]),
types.identifier(fetchBaseUrl),
...(routeType === 'static' ? [] : [types.identifier(resourceRoute)]),
]
)
}

const resolveResourceValue = (value: UIDLStaticValue | UIDLENVValue) => {
const resolveResourceValue = (value: ResourceValue) => {
if (!value) {
return ''
}

if (value.type === 'static') {
return value.content
return value.value
}

return `process.env.${value.content}`
return `process.env.${value.value}` || value.fallback
}
Loading

0 comments on commit 1ec82a1

Please sign in to comment.