Skip to content

Commit 86acab8

Browse files
authored
chore: Allow PascalCase as Modulename (#567)
* feat/fix: Allow PascalCase Types * feat: Allow Pascalcase Keys * feat/fix: Disperse moduleName and Type for relationsships * test: Add/Adjust Tests * doc: Add changelog * chore: Linting * fix: Use Type to access Store module in relationships * fix: Correct data structure * feat: Only use the Type-Name. No more upper-/lowercase transformation anywhere * chore: remove commented out code and console logs * fix: Revert wrong changes * fix: Revert wrong changes
1 parent dcb73cf commit 86acab8

File tree

12 files changed

+127
-28
lines changed

12 files changed

+127
-28
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# next version
22

3+
- **BREAKING**: The response data now allows PascalCase Types and Keys.
4+
With that, If the Type has been pascal case before, but Your module was
5+
in camel case, it worked, because the Type was transformed to camel case
6+
automatically. Now, the Type is not transformed anymore, so you have to
7+
adjust your module to the correct case.
8+
39
# v0.1
410

511
- **BREAKING**: Vue has been removed as a direct dependency.

src/api/ResourcefulApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class ResourcefulApi extends Api {
115115
}
116116

117117
if (json.data) {
118-
parsedResponse.data = normalize(json)
118+
parsedResponse.data = normalize(json, { camelizeTypeValues: false, camelizeKeys: false })
119119
}
120120

121121
if (json.errors) {

src/module/ModuleBuilder.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export class ModuleBuilder {
120120
*/
121121
buildMutations () {
122122
const resourceBuilder = new ResourceBuilder(this.api.store)
123-
124123
const mutations = {
125124
resetItems: resetItemsMutation(this.isCollection),
126125
set: (this.isCollection) ? setAllMutation(resourceBuilder) : setMutation(resourceBuilder),
@@ -179,10 +178,9 @@ export class ModuleBuilder {
179178

180179
for (const relatedObjectType in this.apiMethods.related) {
181180
const relatedObjectMethods = this.apiMethods.related[relatedObjectType]
182-
const relatedObjectTypeActionName = relatedObjectType.charAt(0).toUpperCase() + relatedObjectType.slice(1)
183181

184182
if (relatedObjectMethods.isCollection()) {
185-
const listActionName = `listRelated${relatedObjectTypeActionName}`
183+
const listActionName = `listRelated${relatedObjectType}`
186184
relatedActions[listActionName] = listRelatedAction(this.api, this.moduleName, relatedObjectType)
187185
}
188186
}

src/module/resource/ResourceBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ResourceBuilder {
5656
* Adds Methods (get, load, list) to the relationships for getting / loading them
5757
* Adds Shorthand Methods (rel / loadRel)
5858
*
59-
* @param {Object} jsonResourceObject
59+
* @param {Object} obj jsonResourceObject
6060
*/
6161
buildRelationshipMethods (obj) {
6262
const relationships = obj.relationships

src/module/resource/getRelationship.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
*
33
* @param {Vuex.Store} store
44
* @param {Object} relatedObject
5+
* @param {Object} config{isToMany: boolean}
56
*/
67
export function getRelationship (store, relatedObject, config) {
78
return new Proxy(() => {}, {
89
apply (target, thisArg, argArray) {
9-
const moduleName = relatedObject.data.type
10-
const relatedModule = store.state[moduleName]
10+
const moduleType = relatedObject.data.type
11+
const relatedModule = store.state[moduleType]
1112

1213
if (config.isToMany) {
1314
const [requestedId] = argArray
1415

1516
try {
1617
return relatedModule.items[requestedId]
1718
} catch (e) {
18-
throw new Error(`Related object ${relatedObject.id} not found in ${moduleName}`)
19+
throw new Error(`Related object ${relatedObject.data.id} not found in ${moduleType}`)
1920
}
2021
}
2122

src/module/resource/loadRelationship.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
export function loadRelationship (store, currentObjectId, currentObjectType, relatedObject, config) {
22
return () => {
33
const relatedObjectType = (config.isToMany) ? relatedObject.data[0].type : relatedObject.data.type
4-
const relatedObjectNameForAction = relatedObjectType[0].toUpperCase() + relatedObjectType.slice(1)
54

65
if (config.isToMany) {
7-
return store.dispatch(`${currentObjectType}/listRelated${relatedObjectNameForAction}`, { id: currentObjectId })
6+
return store.dispatch(`${currentObjectType}/listRelated${relatedObjectType}`, { id: currentObjectId })
87
}
98

109
return new Error('Failed to load relationship')

src/route/JsonApiRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class JsonApiRouter extends Router {
7474
}
7575

7676
parseApiResult (data) {
77-
for (const routeResource of Object.values(data.vuexJsonApiRoute)) {
77+
for (const routeResource of Object.values(data.VuexJsonApiRoute)) {
7878
this.addRoute(new JsonApiRoute(routeResource))
7979
}
8080
}

test/api/ResourcefulApi.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test('reads the initial module list', () => {
5151

5252
api.registerModule = registerModuleMock
5353

54-
api.setupApiModules(['foo', 'bar', 'baz'])
54+
api.setupApiModules(['foo', 'bar', 'Baz'])
5555

5656
expect(registerModuleMock.mock.calls.length).toBe(3)
5757
})

test/apiMock.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export function initApiMockServer () {
3535
fetchMock.reset()
3636
fetchMock.config.sendAsJson = false
3737

38-
function book (id) {
38+
function Author (id) {
3939
return {
40-
type: 'Book',
40+
type: 'Author',
4141
id,
4242
attributes: {
4343
author: faker.person.fullName(),
@@ -46,10 +46,39 @@ export function initApiMockServer () {
4646
}
4747
}
4848

49+
function book (id) {
50+
return {
51+
type: 'book',
52+
id,
53+
attributes: {
54+
author: faker.name.findName(),
55+
title: faker.lorem.words(3)
56+
}
57+
}
58+
}
59+
60+
function category (id) {
61+
return {
62+
type: 'Category',
63+
id,
64+
attributes: {
65+
author: faker.name.findName(),
66+
title: faker.lorem.words(3)
67+
}
68+
}
69+
}
70+
71+
fetchMock.get(url('/Author/1'), response(Author(1)))
4972
fetchMock.get(url('/book/1'), response(book(1)))
73+
fetchMock.get(url('/category/1'), response(category(1)))
74+
5075
fetchMock.getOnce(url('/book/1/nometa'), response(book(1), null))
5176
fetchMock.getOnce(url('/book/1/nolinks'), response(book(1), {}, null))
77+
78+
fetchMock.get(url('/Author/'), response([Author(1), Author(2), Author(3)]))
5279
fetchMock.get(url('/book/'), response([book(1), book(2), book(3)]))
80+
fetchMock.get(url('/category/'), response([category(1), category(2), category(3)]))
81+
5382
fetchMock.postOnce(url('/book/'), response(book('new'), {}, {}, 201))
5483
fetchMock.postOnce({
5584
url: url('/clientBook/'),
@@ -97,23 +126,33 @@ export function initApiMock () {
97126
initApiMockServer()
98127

99128
const router = new Router()
129+
router
130+
.addRoute(new Route('Author', 'get', '/Author/{id}', ['id']))
131+
.addRoute(new Route('Author', 'delete', '/Author/{id}', ['id']))
132+
.addRoute(new Route('Author', 'list', '/Author/', []))
133+
.addRoute(new Route('Author', 'create', '/Author/', []))
100134
router
101135
.addRoute(new Route('book', 'get', '/book/{id}', ['id']))
102136
.addRoute(new Route('book', 'delete', '/book/{id}', ['id']))
103137
.addRoute(new Route('book', 'list', '/book/', []))
104138
.addRoute(new Route('book', 'create', '/book/', []))
139+
router
140+
.addRoute(new Route('Category', 'get', '/category/{id}', ['id']))
141+
.addRoute(new Route('Category', 'delete', '/category/{id}', ['id']))
142+
.addRoute(new Route('Category', 'list', '/category/', []))
143+
.addRoute(new Route('Category', 'create', '/category/', []))
105144

106145
const store = new Vuex.Store()
107146
const api = new ResourcefulApi()
108147

109148
api.setBaseUrl('http://api/')
110149
api.setStore(store)
111150
api.setupResourcefulRequests(router)
112-
api.setupApiModules(['book'])
151+
api.setupApiModules(['Author', 'book', 'Category'])
113152

114153
return api
115154
}
116155

117-
export function getVuexContextForResourceType (api, type) {
118-
return api.store._modules.root._children[type].context
156+
export function getVuexContextForResourceType (api, moduleName) {
157+
return api.store._modules.root._children[moduleName].context
119158
}

test/module/actions/createAction.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ const action = createAction(api, 'book')
66
const bookModule = getVuexContextForResourceType(api, 'book')
77

88
test.skip('creates without id', async () => {
9-
action(bookModule, { type: 'Book', attributes: {} })
9+
action(bookModule, { type: 'book', attributes: {} })
1010
})

0 commit comments

Comments
 (0)