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

Refactor adapters #76

Merged
merged 16 commits into from
Jun 7, 2024
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
node_modules
bower_components
dist
example/ng-admin/bower-components
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"singleQuote": true
}
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

install:
@npm install
@bower install

build-dev:
@NODE_ENV=development npm run build
Expand Down
3 changes: 2 additions & 1 deletion example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export const App = ({ dataProvider }: { dataProvider: DataProvider }) => {
);
};

import { Edit, ReferenceInput, SimpleForm, TextInput } from 'react-admin';
import { ReferenceInput, SimpleForm, TextInput } from 'react-admin';
import authProvider from './authProvider';

// The default value for the title field should cause a server validation error as it's not unique
export const BookCreate = () => (
<Create>
<SimpleForm>
Expand Down
39 changes: 3 additions & 36 deletions example/fetchMock.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
import fetchMock from 'fetch-mock';
import { FetchMockServer, withDelay } from '../src';
import { FetchMockServer } from '../src';
import { data } from './data';
import { dataProvider as defaultDataProvider } from './dataProvider';
import { middlewares } from './middlewares';

export const initializeFetchMock = () => {
const restServer = new FetchMockServer({
baseUrl: 'http://localhost:3000',
data,
loggingEnabled: true,
middlewares,
});
if (window) {
// @ts-ignore
window.restServer = restServer; // give way to update data in the console
}

restServer.addMiddleware(withDelay(300));
restServer.addMiddleware(async (request, context, next) => {
if (!request.headers?.get('Authorization')) {
return {
status: 401,
headers: {},
};
}
return next(request, context);
});
restServer.addMiddleware(async (request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.database.getCount(context.collection, {
filter: {
title: context.requestBody?.title,
},
}) > 0
) {
throw new Response(
JSON.stringify({
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
}),
{
status: 400,
statusText: 'Title is required',
},
);
}
}

return next(request, context);
});
fetchMock.mock('begin:http://localhost:3000', restServer.getHandler());
};

Expand Down
35 changes: 35 additions & 0 deletions example/middlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { withDelay } from '../src';
import { data } from './data';

export const middlewares = [
withDelay(300),
async (context, next) => {
if (!context.headers?.get('Authorization')) {
return {
status: 401,
headers: {},
};
}
return next(context);
},
async (context, next) => {
if (context.collection === 'books' && context.method === 'POST') {
if (
data[context.collection].some(
(book) => book.title === context.requestBody?.title,
)
) {
return {
body: {
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
},
status: 400,
headers: {},
};
}
}
return next(context);
},
];
52 changes: 8 additions & 44 deletions example/msw.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,16 @@
import { setupWorker } from 'msw/browser';
import { MswServer, withDelay } from '../src';
import { getMswHandler } from '../src';
import { data } from './data';
import { dataProvider as defaultDataProvider } from './dataProvider';

const restServer = new MswServer({
baseUrl: 'http://localhost:3000',
data,
});

restServer.addMiddleware(withDelay(300));
restServer.addMiddleware(async (request, context, next) => {
if (!request.headers?.get('Authorization')) {
return {
status: 401,
headers: {},
};
}
return next(request, context);
});

restServer.addMiddleware(async (request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.database.getCount(context.collection, {
filter: {
title: context.requestBody?.title,
},
}) > 0
) {
throw new Response(
JSON.stringify({
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
}),
{
status: 400,
statusText: 'Title is required',
},
);
}
}

return next(request, context);
});
import { middlewares } from './middlewares';

export const initializeMsw = async () => {
const worker = setupWorker(restServer.getHandler());
const handler = getMswHandler({
baseUrl: 'http://localhost:3000',
data,
middlewares,
});
const worker = setupWorker(handler);
return worker.start({
quiet: true, // Instruct MSW to not log requests in the console
onUnhandledRequest: 'bypass', // Instruct MSW to ignore requests we don't handle
Expand Down
44 changes: 5 additions & 39 deletions example/sinon.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,16 @@
import sinon from 'sinon';
import { SinonServer, withDelay } from '../src';
import { data } from './data';
import { HttpError, type Options } from 'react-admin';
import simpleRestProvider from 'ra-data-simple-rest';
import { HttpError, type Options } from 'react-admin';
import { SinonServer } from '../src';
import { data } from './data';
import { middlewares } from './middlewares';

export const initializeSinon = () => {
const restServer = new SinonServer({
baseUrl: 'http://localhost:3000',
data,
loggingEnabled: true,
});

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

return next(request, context);
});

restServer.addMiddleware(async (request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.database.getCount(context.collection, {
filter: {
title: context.requestBody?.title,
},
}) > 0
) {
return {
status: 400,
headers: {},
body: {
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
},
};
}
}

return next(request, context);
middlewares,
});

// use sinon.js to monkey-patch XmlHttpRequest
Expand Down
Loading
Loading