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

support for getting nested resources #1606

Open
wants to merge 1 commit into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ await test('createApp', async (t) => {
// URLs
const POSTS = '/posts'
const POSTS_WITH_COMMENTS = '/posts?_embed=comments'
const POSTS_NESTED_WITH_COMMENTS = '/posts/1/comments'
const POST_1 = '/posts/1'
const POST_NOT_FOUND = '/posts/-1'
const POST_WITH_COMMENTS = '/posts/1?_embed=comments'
Expand All @@ -76,6 +77,7 @@ await test('createApp', async (t) => {
// API
{ method: 'GET', url: POSTS, statusCode: 200 },
{ method: 'GET', url: POSTS_WITH_COMMENTS, statusCode: 200 },
{ method: 'GET', url: POSTS_NESTED_WITH_COMMENTS, statusCode: 200 },
{ method: 'GET', url: POST_1, statusCode: 200 },
{ method: 'GET', url: POST_NOT_FOUND, statusCode: 404 },
{ method: 'GET', url: POST_WITH_COMMENTS, statusCode: 200 },
Expand Down
9 changes: 8 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Low } from 'lowdb'
import { json } from 'milliparsec'
import sirv from 'sirv'

import { Data, isItem, Service } from './service.js'
import { Data, getNestedName, isItem, Service } from './service.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
const isProduction = process.env['NODE_ENV'] === 'production'
Expand Down Expand Up @@ -68,6 +68,13 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
next()
})

app.get('/:name/:id/:nested_name', (req, res, next) => {
const { name = '', id = '', nested_name = '' } = req.params

res.locals['data'] = service.find(nested_name, { ...req.query, [getNestedName(name)]: id})
next()
})

app.post('/:name', async (req, res, next) => {
const { name = '' } = req.params
if (isItem(req.body)) {
Expand Down
4 changes: 4 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function isItem(obj: unknown): obj is Item {
return typeof obj === 'object' && obj !== null
}

export function getNestedName(name: string): string {
return name[name.length-1] === 's' ? `${name.substring(0, name.length-1)}Id`: `${name}Id`;
}

export function isData(obj: unknown): obj is Record<string, Item[]> {
if (typeof obj !== 'object' || obj === null) {
return false
Expand Down