Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Found a bug. Anyways, here's the deal:
Browse files Browse the repository at this point in the history
1. It's getting close to API stability.
2. After API stability come the UAT tests
   and the whole big gun quality control.
3. Once it's black-box tested inside & out
   we can finally clean up the code under
   the hood.
4. After cleanin' up, it'll be a release of
   version 1.0.0
  • Loading branch information
adam-rocska committed Jun 7, 2023
1 parent 57d135c commit 87d7ec1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion next-rest/description/Delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ReadHandler} from '#handlers/ReadHandler';
import {Identifier} from '#Identifier';
import {Coded} from './Coded';
import {Description} from './Description';
import {IdentityAware} from './Identified';
import {IdentityAware} from './IdentityAware';

/**
* @summary
Expand Down
19 changes: 0 additions & 19 deletions next-rest/description/Identified.ts

This file was deleted.

38 changes: 38 additions & 0 deletions next-rest/description/IdentityAware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {Description} from '#description';

/**
* @summary
* Describes an identified resource.
* @description
* Used for `Read` and `Delete` mostly but not exclusively.
* Apply this type for cases where you would want to support
* an identifier parameter.
* @group Core API
*/
export type IdentityAware = {
/**
* @summary
* The name of the identifier parameter.
* @description
* This is the name of the parameter that will be used to
* extract the resource's identifier from the NextJS request.
*/
idParameterName: string;
};

/**
* @summary
* Determines if a description is identity aware.
* @param v The description to test.
* @returns True if the description is identity aware, false otherwise.
* @template R The type of the resource.
* @template I The type of the identifier.
* @template Q The type of the query.
* @group Core API
*/
export const isIdentityAware = <R, I, Q>(
v: Description<R, I, Q>
): v is IdentityAware & Description<R, I, Q> => {
const candidate = v as IdentityAware;
return typeof candidate.idParameterName === 'string';
};
2 changes: 1 addition & 1 deletion next-rest/description/Read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ReadHandler} from '#handlers/ReadHandler';
import {Identifier} from '#Identifier';
import {Coded} from './Coded';
import {Description} from './Description';
import {IdentityAware} from './Identified';
import {IdentityAware} from './IdentityAware';

/**
* @summary
Expand Down
2 changes: 1 addition & 1 deletion next-rest/description/Update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Identifier} from '#Identifier';
import {UpdateHandler} from '#handlers/UpdateHandler';
import {Coded} from './Coded';
import {Description} from './Description';
import {IdentityAware} from './Identified';
import {IdentityAware} from './IdentityAware';

/**
* @summary
Expand Down
2 changes: 1 addition & 1 deletion next-rest/description/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export * from './ReadSet';
export * from './Update';
export * from './Delete';
export * from './Queried';
export * from './Identified';
export * from './IdentityAware';
export * from './Description';
export * from './Coded';
7 changes: 6 additions & 1 deletion next-rest/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Description,
IdentityAware,
isDeletable,
isIdentityAware,
isReadable,
isReadSetable,
isUpdatable,
Expand All @@ -22,7 +23,11 @@ export const handler: Handler = description => async (request, response) => {
return response.status(400).end();
if (!isHttpMethod(request.method)) return response.status(405).end();

const isSingular = 'id' in request.query;
const queryKeys = Object.keys(request.query);
const isSingular =
isIdentityAware(description) &&
queryKeys.length === 1 &&
queryKeys[0] === description.idParameterName;

if (request.method === 'GET') {
if (isSingular) {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ describe('handler', () => {
it.each(combine([readSetable, creatable, updatable, deletable]))(
'Should respond with a status 405 when the parameter count is one but it is not a Readable resource',
(descriptor: any) => {
handler(descriptor)({...request, query: {test: 1}}, mockResponse);
handler({...descriptor, idParameterName: 'test'})(
{...request, query: {test: 1}},
mockResponse
);
expect(readHandler).not.toHaveBeenCalled();
expect(readSetHandler).not.toHaveBeenCalled();
expect(createHandler).not.toHaveBeenCalled();
Expand Down

0 comments on commit 87d7ec1

Please sign in to comment.