Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit daae6a4

Browse files
committed
refactor: add functions to create providers
1 parent c47108b commit daae6a4

File tree

1 file changed

+57
-54
lines changed

1 file changed

+57
-54
lines changed

src/providers.js

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ import { Axios } from "@data-provider/axios";
1414

1515
import TAG from "./tag";
1616

17-
function addByIdQuery(model) {
18-
model.addQuery("byId", (id) => ({
17+
function getModelMethod(methodName) {
18+
return methodName || "byId";
19+
}
20+
21+
function getModelParam(param) {
22+
return param || "id";
23+
}
24+
25+
function addModelQuery(model, methodName, modelGetterParam) {
26+
const method = getModelMethod(methodName);
27+
const param = getModelParam(modelGetterParam);
28+
model.addQuery(method, (paramValue) => ({
1929
urlParams: {
20-
id,
30+
[param]: paramValue,
2131
},
2232
}));
2333
}
@@ -31,28 +41,47 @@ function initialState(data) {
3141
};
3242
}
3343

34-
function createRestEntityOrigins({ id, baseUrl }) {
35-
const collectionOrigin = new Axios({
44+
function createCollectionOrigin({ id, url }) {
45+
return new Axios({
3646
id,
37-
url: baseUrl,
47+
url: url,
3848
tags: [TAG],
3949
...initialState([]),
4050
});
51+
}
4152

42-
const modelOrigin = new Axios({
53+
function createModelOrigin({ id, baseUrl, modelId }) {
54+
const modelUrlId = modelId || ":id";
55+
console.log(modelUrlId);
56+
return new Axios({
4357
id: `${id}-model`,
44-
url: `${baseUrl}/:id`,
58+
url: `${baseUrl}/${modelUrlId}`,
4559
tags: [TAG],
4660
...initialState({}),
4761
});
62+
}
4863

49-
addByIdQuery(modelOrigin);
50-
51-
const modelGetter = (modelId) => {
52-
return modelOrigin.queries.byId(modelId);
64+
function modelGetter(modelOrigin, methodName) {
65+
const method = getModelMethod(methodName);
66+
return (modelId) => {
67+
return modelOrigin.queries[method](modelId);
5368
};
69+
}
70+
71+
function createRestEntityOrigins({ id, baseUrl, modelId, modelGetterMethod, modelGetterParam }) {
72+
const collectionOrigin = createCollectionOrigin({
73+
id,
74+
url: baseUrl,
75+
});
76+
77+
const modelOrigin = createModelOrigin({
78+
id,
79+
baseUrl,
80+
modelId,
81+
});
5482

55-
return [collectionOrigin, modelOrigin, modelGetter];
83+
addModelQuery(modelOrigin, modelGetterMethod, modelGetterParam);
84+
return [collectionOrigin, modelOrigin, modelGetter(modelOrigin, modelGetterMethod)];
5685
}
5786

5887
export const about = new Axios({
@@ -93,54 +122,28 @@ export const routesVariants = routesVariantsOrigins[0];
93122
export const routesVariantsModel = routesVariantsOrigins[1];
94123
export const routeVariant = routesVariantsOrigins[2];
95124

96-
const customRoutesVariantsOrigins = createRestEntityOrigins({
125+
export const customRoutesVariants = createCollectionOrigin({
97126
id: "custom-routes-variants",
98-
baseUrl: MOCK_CUSTOM_ROUTES_VARIANTS,
127+
url: MOCK_CUSTOM_ROUTES_VARIANTS,
99128
});
100-
export const customRoutesVariants = customRoutesVariantsOrigins[0];
101129

102130
// Legacy methods
103131

104-
export const behaviors = new Axios({
132+
const behaviorsOrigins = createRestEntityOrigins({
105133
id: "behaviors",
106-
url: `${LEGACY}/${BEHAVIORS}`,
107-
tags: [TAG],
108-
...initialState([]),
109-
});
110-
111-
export const behaviorsModel = new Axios({
112-
id: "behaviors-model",
113-
url: `${LEGACY}/${BEHAVIORS}/:name`,
114-
tags: [TAG],
115-
...initialState({}),
134+
baseUrl: `${LEGACY}/${BEHAVIORS}`,
135+
modelId: ":name",
136+
modelGetterMethod: "byName",
137+
modelGetterParam: "name",
116138
});
139+
export const behaviors = behaviorsOrigins[0];
140+
export const behaviorsModel = behaviorsOrigins[1];
141+
export const behavior = behaviorsOrigins[2];
117142

118-
behaviorsModel.addQuery("byName", (name) => ({
119-
urlParams: {
120-
name,
121-
},
122-
}));
123-
124-
export const behavior = (name) => {
125-
return behaviorsModel.queries.byName(name);
126-
};
127-
128-
export const fixtures = new Axios({
143+
const fixturesOrigins = createRestEntityOrigins({
129144
id: "fixtures",
130-
url: `${LEGACY}/${FIXTURES}`,
131-
tags: [TAG],
132-
...initialState([]),
145+
baseUrl: `${LEGACY}/${FIXTURES}`,
133146
});
134-
135-
export const fixturesModel = new Axios({
136-
id: "fixtures-model",
137-
url: `${LEGACY}/${FIXTURES}/:id`,
138-
tags: [TAG],
139-
...initialState({}),
140-
});
141-
142-
addByIdQuery(fixturesModel);
143-
144-
export const fixture = (id) => {
145-
return fixturesModel.queries.byId(id);
146-
};
147+
export const fixtures = fixturesOrigins[0];
148+
export const fixturesModel = fixturesOrigins[1];
149+
export const fixture = fixturesOrigins[2];

0 commit comments

Comments
 (0)