-
-
Notifications
You must be signed in to change notification settings - Fork 127
Configurable REST API id divider #1785
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b0de9c3
Make idDivider configurable
thomassnielsen e127e04
Make REST API ID divider configurable
thomassnielsen 7c5940b
a few fixes:
ymc9 c002fa2
Expose urlSegmentNameCharSet option
thomassnielsen 5074314
Fixes based on coderabbit review
thomassnielsen 0154a61
chore: rename option
ymc9 2f192be
fix: add missing await in test
ymc9 4fa960f
fix test
ymc9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ import { CrudFailureReason, type ModelMeta } from '@zenstackhq/runtime'; | |
| import { loadSchema, run } from '@zenstackhq/testtools'; | ||
| import { Decimal } from 'decimal.js'; | ||
| import SuperJSON from 'superjson'; | ||
| import makeHandler, { idDivider } from '../../src/api/rest'; | ||
| import makeHandler from '../../src/api/rest'; | ||
|
|
||
| const idDivider = '_'; | ||
|
|
||
| describe('REST server tests', () => { | ||
| let prisma: any; | ||
|
|
@@ -2519,4 +2521,97 @@ describe('REST server tests', () => { | |
| expect(Buffer.isBuffer(included.attributes.bytes)).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('REST server tests - compound id with custom separator', () => { | ||
| const schema = ` | ||
| enum Role { | ||
| COMMON_USER | ||
| ADMIN_USER | ||
| } | ||
|
|
||
| model User { | ||
| email String | ||
| role Role | ||
|
|
||
| @@id([email, role]) | ||
| } | ||
| `; | ||
| const idDivider = ':'; | ||
|
|
||
| beforeAll(async () => { | ||
| const params = await loadSchema(schema); | ||
|
|
||
| prisma = params.enhanceRaw(params.prisma, params); | ||
| zodSchemas = params.zodSchemas; | ||
| modelMeta = params.modelMeta; | ||
|
|
||
| const _handler = makeHandler({ endpoint: 'http://localhost/api', pageSize: 5, idDivider }); | ||
| handler = (args) => | ||
| _handler({ ...args, zodSchemas, modelMeta, url: new URL(`http://localhost/${args.path}`) }); | ||
| }); | ||
|
|
||
| it('POST', async () => { | ||
| const r = await handler({ | ||
| method: 'post', | ||
| path: '/user', | ||
| query: {}, | ||
| requestBody: { | ||
| data: { | ||
| type: 'user', | ||
| attributes: { email: '[email protected]', role: 'COMMON_USER' }, | ||
| }, | ||
| }, | ||
| prisma, | ||
| }); | ||
|
|
||
| expect(r.status).toBe(201); | ||
| }); | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('GET', async () => { | ||
| await prisma.user.create({ | ||
| data: { email: '[email protected]', role: 'COMMON_USER' }, | ||
| }); | ||
|
|
||
| const r = await handler({ | ||
| method: 'get', | ||
| path: '/user', | ||
| query: {}, | ||
| prisma, | ||
| }); | ||
|
|
||
| expect(r.status).toBe(200); | ||
| expect(r.body.data).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('GET single', async () => { | ||
| await prisma.user.create({ | ||
| data: { email: '[email protected]', role: 'COMMON_USER' }, | ||
| }); | ||
|
|
||
| const r = await handler({ | ||
| method: 'get', | ||
| path: '/user/[email protected]:COMMON_USER', | ||
| query: {}, | ||
| prisma, | ||
| }); | ||
|
|
||
| expect(r.status).toBe(200); | ||
| expect(r.body.data.attributes.email).toBe('[email protected]'); | ||
| }); | ||
|
|
||
| it('PUT', async () => { | ||
| await prisma.user.create({ | ||
| data: { email: '[email protected]', role: 'COMMON_USER' }, | ||
| }); | ||
|
|
||
| const r = await handler({ | ||
| method: 'put', | ||
| path: '/user/[email protected]:COMMON_USER', | ||
ymc9 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| query: {}, | ||
| prisma, | ||
| }); | ||
|
|
||
| expect(r.status).toBe(200); | ||
| }); | ||
| }); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.