Skip to content

Commit

Permalink
Make sinon async compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jun 6, 2024
1 parent 3c655d1 commit 8662b5e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 115 deletions.
25 changes: 14 additions & 11 deletions example/sinon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sinon from 'sinon';
import { SinonServer } from '../src/FakeRest';
import { SinonServer, withDelay } from '../src/FakeRest';
import { data } from './data';
import { HttpError, type Options } from 'react-admin';
import simpleRestProvider from 'ra-data-simple-rest';
Expand All @@ -11,16 +11,19 @@ export const initializeSinon = () => {
loggingEnabled: true,
});

restServer.addMiddleware((request, context, next) => {
restServer.addMiddleware(withDelay(3000));
restServer.addMiddleware(async (request, context, next) => {
if (request.requestHeaders.Authorization === undefined) {
request.respond(401, {}, 'Unauthorized');
return null;
return {
status: 401,
headers: {},
};
}

return next(request, context);
});

restServer.addMiddleware((request, context, next) => {
restServer.addMiddleware(async (request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.collections[context.collection].getCount({
Expand All @@ -29,15 +32,15 @@ export const initializeSinon = () => {
},
}) > 0
) {
request.respond(
401,
{},
JSON.stringify({
return {
status: 400,
headers: {},
body: {
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
}),
);
},
};
}
}

Expand Down
49 changes: 0 additions & 49 deletions src/BaseServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ export abstract class BaseServer<
throw new Error('Not implemented');
}

extractContextSync(
request: RequestType,
): Pick<FakeRestContext, 'url' | 'method' | 'params' | 'requestJson'> {
throw new Error('Not implemented');
}

respondSync(
response: BaseResponse | null,
request: RequestType,
context: FakeRestContext,
): ResponseType {
throw new Error('Not implemented');
}

async handle(request: RequestType): Promise<ResponseType | undefined> {
const context = this.getContext(await this.extractContext(request));

Expand Down Expand Up @@ -70,41 +56,6 @@ export abstract class BaseServer<
}
}

handleSync(request: RequestType): ResponseType | undefined {
const context = this.getContext(this.extractContextSync(request));

// Call middlewares
let index = 0;
const middlewares = [...this.middlewares];

const next = (req: RequestType, ctx: FakeRestContext) => {
const middleware = middlewares[index++];
if (middleware) {
return middleware(req, ctx, next);
}

return this.handleRequest(req, ctx);
};

try {
const response = next(request, context);
if (response instanceof Promise) {
throw new Error(
'Middleware returned a promise in a sync context',
);
}
if (response != null) {
return this.respondSync(response, request, context);
}
} catch (error) {
if (error instanceof Error) {
throw error;
}

return error as ResponseType;
}
}

handleRequest(request: RequestType, ctx: FakeRestContext): BaseResponse {
// Handle Single Objects
for (const name of this.getSingleNames()) {
Expand Down
Loading

0 comments on commit 8662b5e

Please sign in to comment.