Skip to content

Commit a4dda8e

Browse files
committed
fix: _embed param
1 parent a4d829a commit a4dda8e

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/app.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ await new Promise<void>((resolve, reject) => {
5454
await test('createApp', async (t) => {
5555
// URLs
5656
const POSTS = '/posts'
57+
const POSTS_WITH_COMMENTS = '/posts?_embed=comments'
5758
const POST_1 = '/posts/1'
5859
const POST_NOT_FOUND = '/posts/-1'
60+
const POST_WITH_COMMENTS = '/posts/1?_embed=comments'
5961
const COMMENTS = '/comments'
6062
const POST_COMMENTS = '/comments?postId=1'
6163
const NOT_FOUND = '/not-found'
@@ -73,8 +75,10 @@ await test('createApp', async (t) => {
7375

7476
// API
7577
{ method: 'GET', url: POSTS, statusCode: 200 },
78+
{ method: 'GET', url: POSTS_WITH_COMMENTS, statusCode: 200 },
7679
{ method: 'GET', url: POST_1, statusCode: 200 },
7780
{ method: 'GET', url: POST_NOT_FOUND, statusCode: 404 },
81+
{ method: 'GET', url: POST_WITH_COMMENTS, statusCode: 200 },
7882
{ method: 'GET', url: COMMENTS, statusCode: 200 },
7983
{ method: 'GET', url: POST_COMMENTS, statusCode: 200 },
8084
{ method: 'GET', url: OBJECT, statusCode: 200 },

src/service.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export type PaginatedItems = {
4747
data: Item[]
4848
}
4949

50+
function ensureArray(arg: string | string[] = []): string[] {
51+
return Array.isArray(arg) ? arg : [arg]
52+
}
53+
5054
function embed(db: Low<Data>, name: string, item: Item, related: string): Item {
5155
if (inflection.singularize(related) === related) {
5256
const relatedData = db.data[inflection.pluralize(related)] as Item[]
@@ -148,13 +152,13 @@ export class Service {
148152
findById(
149153
name: string,
150154
id: string,
151-
query: { _embed?: string[] },
155+
query: { _embed?: string[] | string },
152156
): Item | undefined {
153157
const value = this.#get(name)
154158

155159
if (Array.isArray(value)) {
156160
let item = value.find((item) => item['id'] === id)
157-
query._embed?.forEach((related) => {
161+
ensureArray(query._embed).forEach((related) => {
158162
if (item !== undefined) item = embed(this.#db, name, item, related)
159163
})
160164
return item
@@ -184,9 +188,10 @@ export class Service {
184188
}
185189

186190
// Include
187-
query._embed?.forEach((related) => {
188-
if (items !== undefined && Array.isArray(items))
191+
ensureArray(query._embed).forEach((related) => {
192+
if (items !== undefined && Array.isArray(items)) {
189193
items = items.map((item) => embed(this.#db, name, item, related))
194+
}
190195
})
191196

192197
// Return list if no query params
@@ -209,11 +214,18 @@ export class Service {
209214
continue
210215
}
211216
if (
212-
['_sort', '_start', '_end', '_limit', '_page', '_per_page'].includes(
213-
key,
214-
)
215-
)
217+
[
218+
'_embed',
219+
'_sort',
220+
'_start',
221+
'_end',
222+
'_limit',
223+
'_page',
224+
'_per_page',
225+
].includes(key)
226+
) {
216227
continue
228+
}
217229
conds[key] = [Condition.default, value]
218230
}
219231

0 commit comments

Comments
 (0)