mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
Merge pull request #67 from bknd-io/fix/api-query-with-string-object
fix/api-query-with-string-object
This commit is contained in:
@@ -123,6 +123,23 @@ describe("data-query-impl", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// over http
|
||||||
|
{
|
||||||
|
const output = { with: { images: {} } };
|
||||||
|
decode({ with: "images" }, output);
|
||||||
|
decode({ with: '["images"]' }, output);
|
||||||
|
decode({ with: ["images"] }, output);
|
||||||
|
decode({ with: { images: {} } }, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const output = { with: { images: {}, comments: {} } };
|
||||||
|
decode({ with: "images,comments" }, output);
|
||||||
|
decode({ with: ["images", "comments"] }, output);
|
||||||
|
decode({ with: '["images", "comments"]' }, output);
|
||||||
|
decode({ with: { images: {}, comments: {} } }, output);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import {
|
|||||||
type StaticDecode,
|
type StaticDecode,
|
||||||
StringEnum,
|
StringEnum,
|
||||||
Type,
|
Type,
|
||||||
Value
|
Value,
|
||||||
|
isObject
|
||||||
} from "core/utils";
|
} from "core/utils";
|
||||||
import { WhereBuilder, type WhereQuery } from "../entities";
|
import { WhereBuilder, type WhereQuery } from "../entities";
|
||||||
|
|
||||||
@@ -71,22 +72,51 @@ export type RepoWithSchema = Record<
|
|||||||
>;
|
>;
|
||||||
|
|
||||||
export const withSchema = <TSelf extends TThis>(Self: TSelf) =>
|
export const withSchema = <TSelf extends TThis>(Self: TSelf) =>
|
||||||
Type.Transform(Type.Union([stringArray, Type.Record(Type.String(), Self)]))
|
Type.Transform(
|
||||||
|
Type.Union([Type.String(), Type.Array(Type.String()), Type.Record(Type.String(), Self)])
|
||||||
|
)
|
||||||
.Decode((value) => {
|
.Decode((value) => {
|
||||||
let _value = typeof value === "string" ? [value] : value;
|
// images
|
||||||
|
// images,comments
|
||||||
|
// ["images","comments"]
|
||||||
|
// { "images": {} }
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (!Array.isArray(value) && isObject(value)) {
|
||||||
if (!value.every((v) => typeof v === "string")) {
|
console.log("is object");
|
||||||
throw new Error("Invalid 'with' schema");
|
return value as RepoWithSchema;
|
||||||
}
|
|
||||||
|
|
||||||
_value = value.reduce((acc, v) => {
|
|
||||||
acc[v] = {};
|
|
||||||
return acc;
|
|
||||||
}, {} as RepoWithSchema);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return _value as RepoWithSchema;
|
let _value: any = null;
|
||||||
|
if (typeof value === "string") {
|
||||||
|
// if stringified object
|
||||||
|
if (value.match(/^\{/)) {
|
||||||
|
return JSON.parse(value) as RepoWithSchema;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if stringified array
|
||||||
|
if (value.match(/^\[/)) {
|
||||||
|
_value = JSON.parse(value) as string[];
|
||||||
|
|
||||||
|
// if comma-separated string
|
||||||
|
} else if (value.includes(",")) {
|
||||||
|
_value = value.split(",");
|
||||||
|
|
||||||
|
// if single string
|
||||||
|
} else {
|
||||||
|
_value = [value];
|
||||||
|
}
|
||||||
|
} else if (Array.isArray(value)) {
|
||||||
|
_value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_value || !Array.isArray(_value) || !_value.every((v) => typeof v === "string")) {
|
||||||
|
throw new Error("Invalid 'with' schema");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _value.reduce((acc, v) => {
|
||||||
|
acc[v] = {};
|
||||||
|
return acc;
|
||||||
|
}, {} as RepoWithSchema);
|
||||||
})
|
})
|
||||||
.Encode((value) => value);
|
.Encode((value) => value);
|
||||||
|
|
||||||
@@ -117,7 +147,7 @@ export type RepoQueryIn = {
|
|||||||
offset?: number;
|
offset?: number;
|
||||||
sort?: string | { by: string; dir: "asc" | "desc" };
|
sort?: string | { by: string; dir: "asc" | "desc" };
|
||||||
select?: string[];
|
select?: string[];
|
||||||
with?: string[] | Record<string, RepoQueryIn>;
|
with?: string | string[] | Record<string, RepoQueryIn>;
|
||||||
join?: string[];
|
join?: string[];
|
||||||
where?: WhereQuery;
|
where?: WhereQuery;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user