Permalink
Browse files

πŸ› @phenomic/core: add missing API entry points for queries without path

  • Loading branch information...
MoOx committed Mar 6, 2018
1 parent d2e740a commit d0572d3ac6c817f64e6cb7680595e5db2e458bbd
Showing with 92 additions and 3 deletions.
  1. +1 βˆ’1 flow/interfaces/phenomic.js
  2. +86 βˆ’0 packages/core/src/api/index.js
  3. +5 βˆ’2 packages/core/src/db/index.js
@@ -37,7 +37,7 @@ export type PhenomicDB = {
) => Promise<PhenomicDBEntry>,
getPartial: (sub: string | Array<string>, id: string) => Promise<mixed>,
getList: (
sub: string | Array<string>,
sub: null | string | Array<string>,
config?: LevelStreamConfig,
filter?: string,
filterValue?: string
@@ -143,6 +143,92 @@ function createServer(db: PhenomicDB, plugins: PhenomicPlugins) {
}
});
server.get("/by-:filter/:value/:order.json", async function(
req: express$Request,
res: express$Response
) {
debug(req.url, JSON.stringify(req.params));
try {
const reverse = req.params.order === "desc";
const list = await db.getList(
null,
{ reverse },
req.params.filter,
req.params.value
);
res.json(connect(list));
} catch (error) {
console.error(error);
res.status(404).end();
}
});
server.get("/by-:filter/:value/:order/limit-:limit.json", async function(
req: express$Request,
res: express$Response
) {
debug(req.url, JSON.stringify(req.params));
try {
const limit = parseInt(req.params.limit);
const reverse = req.params.order === "desc";
const list = await db.getList(
null,
{
limit: limit + 1,
reverse
},
req.params.filter,
req.params.value
);
res.json(connect(list, limit));
} catch (error) {
console.error(error);
res.status(404).end();
}
});
server.get(
"/by-:filter/:value/:order/limit-:limit/after-:after.json",
async function(req: express$Request, res: express$Response) {
debug(req.url, JSON.stringify(req.params));
try {
const limit = parseInt(req.params.limit);
const after = decode(req.params.after);
// @todo check lt validity (exist?); otherwise, trigger an error (404?)
// cause during dev all "lt" are responding 200, even random values
// but in production, it's not the case as only known values are
// generated as endpoints
const reverse = req.params.order === "desc";
const [list, previousList] = await Promise.all([
db.getList(
null,
{
limit: limit + 1,
gte: after,
reverse
},
req.params.filter,
req.params.value
),
db.getList(
null,
{
limit: limit + 1,
gt: after,
reverse: !reverse
},
req.params.filter,
req.params.value
)
]);
res.json(connect(list, limit, previousList));
} catch (error) {
console.error(error);
res.status(404).end();
}
}
);
server.get("/item/*.json", async function(
req: express$Request,
res: express$Response
@@ -158,7 +158,10 @@ const db = {
}
};
},
async getPartial(sub: string | Array<string>, id: string): Promise<mixed> {
async getPartial(
sub: null | string | Array<string>,
id: string
): Promise<mixed> {
const item = getSublevel(sub).find(item => item.id === id);
if (!item) {
return id;
@@ -171,7 +174,7 @@ const db = {
},
getList(
sub: string | Array<string>,
sub: null | string | Array<string>,
config: LevelStreamConfig = {},
filter: string = "default",
filterValue: string = ""

0 comments on commit d0572d3

Please sign in to comment.