Skip to content

Commit

Permalink
impl GET ALL and POST (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kedoska authored Sep 13, 2020
1 parent be74741 commit 5ecb505
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 47 deletions.
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {normalize} from 'path'
import { Router, Request, Response, NextFunction } from 'express'
import { normalize } from 'path'
import { Router, Request, Response, NextFunction, json, urlencoded } from 'express'

export interface Pagination {
limit: number
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface Query {
conditions?: Condition[]
}

export type DataExtractor = <T>(payload: any) => T
export type DataExtractor = (payload: any) => any

export type QueryExtractor = (payload: any) => Query

Expand Down Expand Up @@ -107,6 +107,8 @@ export interface RestOptions {

export default (options: RestOptions): Router => {
const router = Router()
router.use(json())
router.use(urlencoded({ extended: false }))

const parent = !options.parent ? '/' : options.parent
const path = normalize(`${parent}/${options.version}/${options.resourceName}`)
Expand All @@ -126,7 +128,7 @@ export default (options: RestOptions): Router => {
req.dbQuery = options.query.extractor(req[queryExtractorSource])
}

if (options.parser) {
if (req.method === 'POST' && options.parser) {
req.parsedResource = options.parser.extractor(req[options.parser.source])
}

Expand Down Expand Up @@ -175,15 +177,16 @@ export default (options: RestOptions): Router => {
}
})

router.post(`${path}/:id`, async (req, res, next) => {
router.post(`${path}`, async (req, res, next) => {
if (!options.dataAdapter.createOne) {
next(new Error(`createOne not yet implemented`))
return
}

try {
res.send(await options.dataAdapter.createOne(options.resourceName, req.parsedResource))
res.send(await options.dataAdapter.createOne(options.resourceName, req.parsedResource || req.body))
} catch ({ message }) {
next(new Error(`could not create the resource "${req.dbResourceId}", ${message}`))
next(new Error(`could not create the new resource, ${message}`))
}
})

Expand All @@ -205,7 +208,9 @@ export default (options: RestOptions): Router => {
return
}
try {
res.send(await options.dataAdapter.updateOne(options.resourceName, req.dbResourceId, req.parsedResource))
res.send(
await options.dataAdapter.updateOne(options.resourceName, req.dbResourceId, req.parsedResource || req.body),
)
} catch ({ message }) {
next(new Error(`could not update the resource "${req.dbResourceId}", ${message}`))
}
Expand Down
40 changes: 40 additions & 0 deletions tests/GET.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as express from 'express'
import * as request from 'supertest'
import resty, { DataAdapter, RequestDataSource } from '../src'

interface User {
name: string
}
let database: User[] = []

beforeAll(() => {
database = []
})

test('GET ALL', function (done) {
const dataAdapter: DataAdapter = {
selectMany: (resourceName: string): Promise<User[]> =>
new Promise((resolve) => {
resolve(database)
}),
}

const users = resty({
version: 'v1',
resourceName: 'users',
dataAdapter,
})

const app = express()
app.use(users)

request(app)
.get('/v1/users')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err)
expect(res.body).toMatchObject(database)
done()
})
})
45 changes: 45 additions & 0 deletions tests/POST.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as express from 'express'
import * as request from 'supertest'
import resty, { DataAdapter, RequestDataSource } from '../src'

interface User {
name: string
}
let database: User[] = []

beforeAll(() => {
database = []
})

const dataAdapter: DataAdapter = {
createOne: (resourceName: string, resource: User): Promise<User> =>
new Promise((resolve) => {
database.push(resource)
resolve(resource)
}),
}

const users = resty({
version: 'v1',
resourceName: 'users',
dataAdapter,
})

const app = express()
app.use(users)

test('CREATE ONE', function (done) {
const payload = { user: 'marco' }
request(app)
.post('/v1/users')
.send(payload)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err)
expect(res.body).toMatchObject(payload)
expect(database.length).toBe(1)
done()
})
})
39 changes: 0 additions & 39 deletions tests/configure.test.ts

This file was deleted.

0 comments on commit 5ecb505

Please sign in to comment.