Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.8.31 - fixes to lodash-es issues, added variable depth schemaPopulated population #262

Open
wants to merge 9 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions @factor/api/package.json
Expand Up @@ -11,16 +11,15 @@
},
"priority": 30
},
"version": "1.8.30",
"version": "1.8.31",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@types/detect-node": "^2.0.0",
"@types/jquery": "^3.3.31",
"@types/lodash": "^4.14.149",
"@types/lodash-es": "^4.17.3",
"@types/lodash": "^4.14.168",
"@types/markdown-it": "^10.0.0",
"@types/markdown-it-anchor": "^4.0.3",
"@types/sanitize-html": "^1.20.2",
Expand All @@ -35,7 +34,6 @@
"flatted": "^2.0.1",
"front-matter": "^3.1.0",
"jquery": "^3.5.0",
"lodash-es": "^4.17.15",
"markdown-it": "^10.0.0",
"markdown-it-anchor": "^5.0.2",
"markdown-it-implicit-figures": "^0.10.0",
Expand Down
11 changes: 7 additions & 4 deletions @factor/api/post-types.ts
Expand Up @@ -31,7 +31,10 @@ export interface PostTypeConfig {
permissions?: SchemaPermissions
schemaDefinition?: SchemaDefinition | (() => SchemaDefinition)
schemaMiddleware?: (s: Schema) => void
schemaPopulated?: Record<string, PopulationContexts> | string[]
schemaPopulated?:
| Record<string, PopulationContexts>
| string[]
| Array<string | string[]>
schemaOptions?: SchemaOptions
}

Expand All @@ -56,19 +59,19 @@ export const postTypesConfig = (): PostTypeConfig[] => {
nameIndex: label,
nameSingle: label,
namePlural: label,
..._,
..._
}
})
}

export const basePostTypeConfig = (): PostTypeConfig => {
return postTypesConfig().find((pt) => pt.postType == "post") || { postType: "post" }
return postTypesConfig().find(pt => pt.postType == "post") || { postType: "post" }
}

export const getPostTypeConfig = (postType?: string): PostTypeConfig => {
const base = basePostTypeConfig()

if (!postType) return base

return postTypesConfig().find((pt) => pt.postType == postType) || base
return postTypesConfig().find(pt => pt.postType == postType) || base
}
23 changes: 11 additions & 12 deletions @factor/api/utils-lodash.ts
@@ -1,12 +1,11 @@
export {
isEqual,
throttle,
isEmpty,
pickBy,
orderBy,
uniq,
cloneDeep,
merge,
identity,
omit,
} from "lodash-es"
export { default as isEqual } from "lodash/isEqual"
export { default as throttle } from "lodash/throttle"
export { default as isEmpty } from "lodash/isEmpty"
export { default as pickBy } from "lodash/pickBy"
export { default as orderBy } from "lodash/orderBy"
export { default as uniq } from "lodash/uniq"
export { default as cloneDeep } from "lodash/cloneDeep"
export { default as merge } from "lodash/merge"
export { default as identity } from "lodash/identity"
export { default as omit } from "lodash/omit"
export { default as flatten } from "lodash/flatten"
1 change: 0 additions & 1 deletion @factor/app/factor-styles.less
@@ -1,4 +1,3 @@
@import "~@factor/ui/css/standard-reset.less";
.factor-app {
@import "~@factor/ui/css/standard-form.less";
--color-primary: #0471ff;
Expand Down
4 changes: 2 additions & 2 deletions @factor/build/package.json
Expand Up @@ -18,7 +18,7 @@
"@types/cli-progress": "^3.4.0",
"@types/copy-webpack-plugin": "^6.0.0",
"@types/cssnano": "^4.0.0",
"@types/mini-css-extract-plugin": "^0.9.0",
"@types/mini-css-extract-plugin": "^1.2.2",
"@types/optimize-css-assets-webpack-plugin": "^5.0.1",
"@types/postcss-import": "^12.0.0",
"@types/terser-webpack-plugin": "^4.0.0",
Expand All @@ -41,7 +41,7 @@
"less": "^3.9.0",
"less-loader": "^7.0.0",
"markdown-image-loader": "^2.0.0",
"mini-css-extract-plugin": "^0.9.0",
"mini-css-extract-plugin": "^1.3.6",
"null-loader": "^4.0.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"postcss-import": "^12.0.1",
Expand Down
2 changes: 1 addition & 1 deletion @factor/cli/transpile.js
Expand Up @@ -2,7 +2,7 @@

module.exports = () => {
// https://regex101.com/r/La4C28/1
const transpileModules = ["@factor", ".*factor", "lodash-es"]
const transpileModules = ["@factor", ".*factor"]
require("ts-node").register({
transpileOnly: true,
compilerOptions: {
Expand Down
4 changes: 2 additions & 2 deletions @factor/core/package.json
@@ -1,7 +1,7 @@
{
"name": "@factor/core",
"description": "This is the parent package for all Factor core packages.",
"version": "1.8.30",
"version": "1.8.31",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand All @@ -10,7 +10,7 @@
"_id": "core"
},
"dependencies": {
"@factor/api": "^1.8.30",
"@factor/api": "^1.8.31",
"@factor/app": "^1.8.28",
"@factor/attachment": "^1.8.28",
"@factor/build": "^1.8.29",
Expand Down
4 changes: 2 additions & 2 deletions @factor/create-factor-app/package.json
@@ -1,6 +1,6 @@
{
"name": "create-factor-app",
"version": "1.8.26",
"version": "1.8.31",
"description": "Create a Factor App in seconds.",
"bin": "cli.js",
"files": [
Expand All @@ -13,7 +13,7 @@
"chalk": "^4.0.0",
"consola": "^2.6.0",
"figures": "^3.0.0",
"lodash": "^4.17.11",
"lodash": "^4.17.20",
"sao": "^1.6.1",
"superb": "^4.0.0",
"ts-node": "^9.0.0",
Expand Down
3 changes: 0 additions & 3 deletions @factor/dashboard/theme/reset.less
Expand Up @@ -103,9 +103,6 @@ h5,
h6 {
margin-top: 0;
margin-bottom: 0;
font-weight: inherit;
font-size: inherit;
line-height: inherit;
}

article,
Expand Down
2 changes: 0 additions & 2 deletions @factor/dashboard/theme/style.less
@@ -1,5 +1,3 @@
@import "reset.less";

.factor-dashboard {
--breakpoint-xs: 0;
--breakpoint-sm: 576px;
Expand Down
66 changes: 50 additions & 16 deletions @factor/post/request.ts
Expand Up @@ -16,9 +16,10 @@ import {
PostIndex,
PostStatus,
PostIndexConditions,
PopulationContext,
PopulationContext
} from "./types"
import { getSchemaPopulatedFields } from "./util"
import { flatten } from "@factor/api/utils-lodash"

/**
* For lists of posts, we don't want to call/populate on every page nav if not needed
Expand Down Expand Up @@ -49,6 +50,34 @@ export const sendPostRequest = async <T = unknown>(
return await endpointRequest({ id: "posts", method, params })
}

type PopulatedSchemaFields = Record<
string,
Array<Record<string, any>> | Record<string, any> | string
>

const _crawlFields = (
fields: Array<string>,
post: PopulatedSchemaFields,
index: number = 0
): string[] => {
if (index == fields.length) {
return []
}

const field = fields[index]
const ptr = post[field]

if (Array.isArray(ptr)) {
return flatten(ptr.map(t => _crawlFields(fields, t, index + 1)))
} else if (typeof ptr == "string") {
return [ptr]
} else if (ptr instanceof Object) {
return _crawlFields(fields, ptr as PopulatedSchemaFields, index + 1)
} else {
return []
}
}

/**
* Populates fields in a post as determined by its Factor schema
* @param posts - posts to populate
Expand All @@ -60,7 +89,7 @@ export const sendPostRequest = async <T = unknown>(
export const requestPostPopulate = async <T extends FactorPostKey>({
posts,
depth = 10,
context = PopulationContext.Any,
context = PopulationContext.Any
}: {
posts: T[]
depth?: number
Expand All @@ -74,11 +103,16 @@ export const requestPostPopulate = async <T extends FactorPostKey>({
const populatedFields = getSchemaPopulatedFields({
postType: post.postType ?? "post",
depth,
context,
context
})

populatedFields.forEach((field) => {
const v = post[field]
populatedFields.forEach((field: String | Array<string>) => {
var v
if (typeof field == "string") {
v = post[field]
} else if (Array.isArray(field)) {
v = _crawlFields(field, post)
}
if (v) {
if (Array.isArray(v)) {
_ids = [..._ids, ...v]
Expand All @@ -95,7 +129,7 @@ export const requestPostPopulate = async <T extends FactorPostKey>({

if (_idsFiltered.length > 0) {
const posts = (await sendPostRequest("populatePosts", {
_ids: _idsFiltered,
_ids: _idsFiltered
})) as FactorPost[]

await requestPostPopulate({ posts, depth, context })
Expand All @@ -113,7 +147,7 @@ export const handlePostPopulation = async (
post: FactorPostState,
{
depth = 10,
context = PopulationContext.Any,
context = PopulationContext.Any
}: { depth?: number; context?: PopulationContext } = {}
): Promise<void> => {
if (post) {
Expand All @@ -135,7 +169,7 @@ export const handlePostPopulation = async (
*/
export const requestPostSave = async <T extends FactorPostState | never>({
post,
postType,
postType
}: UpdatePost): Promise<T> => {
const _post = await sendPostRequest<T>("savePost", { data: post, postType })

Expand Down Expand Up @@ -168,7 +202,7 @@ export const requestEmbeddedPost = async <T extends FactorPostState | never>(
export const requestPostSaveMany = async ({
_ids,
data,
postType,
postType
}: UpdateManyPosts): Promise<FactorPost[]> => {
setLocalPostTypeCache(postType)
const result = await sendPostRequest("updateManyById", { data, _ids, postType })
Expand All @@ -178,7 +212,7 @@ export const requestPostSaveMany = async ({

export const requestPostDeleteMany = async ({
_ids,
postType,
postType
}: UpdateManyPosts): Promise<FactorPost[]> => {
setLocalPostTypeCache(postType)

Expand All @@ -199,7 +233,7 @@ export const requestPostSingle = async (
token,
createOnEmpty = false,
status = "all",
conditions = {},
conditions = {}
} = _arguments

const params: PostRequestParameters = { postType, createOnEmpty, status, log }
Expand Down Expand Up @@ -244,7 +278,7 @@ export const requestPostIndex = async (
search,
cache = true,
sameSource = false,
conditions = {},
conditions = {}
} = _arguments

let { storeKey } = _arguments
Expand All @@ -267,11 +301,11 @@ export const requestPostIndex = async (
conditions,
postType,
options: { limit, skip, page, sort, order, time, search },
sameSource,
sameSource
}

const taxonomies: (keyof PostIndexConditions)[] = ["tag", "category", "status", "role"]
taxonomies.forEach((_) => {
taxonomies.forEach(_ => {
if (_arguments[_]) params.conditions[_] = _arguments[_]
})

Expand All @@ -289,8 +323,8 @@ export const requestPostIndex = async (
storeItem(storeKey, { posts, meta })

const embeddedPosts = posts
.map((p) => (p.embedded?.length ? p.embedded[0] : undefined))
.filter((_) => _) as FactorPost[] // remove undefined, but typescript doesn't understand that
.map(p => (p.embedded?.length ? p.embedded[0] : undefined))
.filter(_ => _) as FactorPost[] // remove undefined, but typescript doesn't understand that

await requestPostPopulate({ posts: [...posts, ...embeddedPosts] })

Expand Down
3 changes: 0 additions & 3 deletions @factor/ui/css/standard-reset.less
Expand Up @@ -103,9 +103,6 @@ h5,
h6 {
margin-top: 0;
margin-bottom: 0;
font-weight: inherit;
font-size: inherit;
line-height: inherit;
}

article,
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Expand Up @@ -27,7 +27,7 @@ module.exports = {
"__SRC__/(.*)$": "<rootDir>/test/modules/alias/$1",
},

transformIgnorePatterns: ["node_modules/(?!(@factor|factor|lodash-es))"],
transformIgnorePatterns: ["node_modules/(?!(@factor|factor))"],

transform: {
"^.+\\.ts$": "ts-jest",
Expand Down