| 1 |
import assert from "../assert"; |
| 2 |
import { camelize, dasherize } from "../utils/inflector"; |
| 3 |
import HasMany from "../orm/associations/has-many"; |
| 4 |
|
| 5 |
const pathModelClassCache = {}; |
| 6 |
|
| 7 |
/** |
| 8 |
@hide |
| 9 |
*/ |
| 10 |
export default class BaseRouteHandler { |
| 11 |
getModelClassFromPath(fullPath) { |
| 12 |
if (!fullPath) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
if (typeof pathModelClassCache[fullPath] !== "string") { |
| 17 |
let path = fullPath.split("/"); |
| 18 |
let lastPath; |
| 19 |
|
| 20 |
for (let i = path.length - 1; i >= 0; i--) { |
| 21 |
const segment = path[i]; |
| 22 |
if (segment.length && segment[0] !== ":") { |
| 23 |
lastPath = segment; |
| 24 |
break; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
pathModelClassCache[fullPath] = dasherize( |
| 29 |
camelize(this._container.inflector.singularize(lastPath)) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
return pathModelClassCache[fullPath]; |
| 34 |
} |
| 35 |
|
| 36 |
_getIdForRequest(request, jsonApiDoc) { |
| 37 |
let id; |
| 38 |
if (request && request.params && request.params.id) { |
| 39 |
id = request.params.id; |
| 40 |
} else if (jsonApiDoc && jsonApiDoc.data && jsonApiDoc.data.id) { |
| 41 |
id = jsonApiDoc.data.id; |
| 42 |
} |
| 43 |
return id; |
| 44 |
} |
| 45 |
|
| 46 |
_getJsonApiDocForRequest(request, modelName) { |
| 47 |
let body; |
| 48 |
if (request && request.requestBody) { |
| 49 |
body = JSON.parse(request.requestBody); |
| 50 |
} |
| 51 |
return this.serializerOrRegistry.normalize(body, modelName); |
| 52 |
} |
| 53 |
|
| 54 |
_getAttrsForRequest(request, modelName) { |
| 55 |
let json = this._getJsonApiDocForRequest(request, modelName); |
| 56 |
let id = this._getIdForRequest(request, json); |
| 57 |
let attrs = {}; |
| 58 |
|
| 59 |
assert( |
| 60 |
json.data && |
| 61 |
(json.data.attributes || json.data.type || json.data.relationships), |
| 62 |
`You're using a shorthand or #normalizedRequestAttrs, but your serializer's normalize function did not return a valid JSON:API document. Consult the docs for the normalize hook on the Serializer class.` |
| 63 |
); |
| 64 |
|
| 65 |
if (json.data.attributes) { |
| 66 |
attrs = Object.keys(json.data.attributes).reduce((sum, key) => { |
| 67 |
sum[camelize(key)] = json.data.attributes[key]; |
| 68 |
return sum; |
| 69 |
}, {}); |
| 70 |
} |
| 71 |
|
| 72 |
if (json.data.relationships) { |
| 73 |
Object.keys(json.data.relationships).forEach((relationshipName) => { |
| 74 |
let relationship = json.data.relationships[relationshipName]; |
| 75 |
let modelClass = this.schema.modelClassFor(modelName); |
| 76 |
let association = modelClass.associationFor(camelize(relationshipName)); |
| 77 |
let valueForRelationship; |
| 78 |
|
| 79 |
assert( |
| 80 |
association, |
| 81 |
`You're passing the relationship '${relationshipName}' to the '${modelName}' model via a ${request.method} to '${request.url}', but you did not define the '${relationshipName}' association on the '${modelName}' model.` |
| 82 |
); |
| 83 |
|
| 84 |
if (association.isPolymorphic) { |
| 85 |
valueForRelationship = relationship.data; |
| 86 |
} else if (association instanceof HasMany) { |
| 87 |
valueForRelationship = |
| 88 |
relationship.data && relationship.data.map((rel) => rel.id); |
| 89 |
} else { |
| 90 |
valueForRelationship = relationship.data && relationship.data.id; |
| 91 |
} |
| 92 |
|
| 93 |
attrs[association.identifier] = valueForRelationship; |
| 94 |
}, {}); |
| 95 |
} |
| 96 |
|
| 97 |
if (id) { |
| 98 |
attrs.id = id; |
| 99 |
} |
| 100 |
|
| 101 |
return attrs; |
| 102 |
} |
| 103 |
|
| 104 |
_getAttrsForFormRequest({ requestBody }) { |
| 105 |
let attrs; |
| 106 |
let urlEncodedParts = []; |
| 107 |
|
| 108 |
assert( |
| 109 |
requestBody && typeof requestBody === "string", |
| 110 |
`You're using the helper method #normalizedFormData, but the request body is empty or not a valid url encoded string.` |
| 111 |
); |
| 112 |
|
| 113 |
urlEncodedParts = requestBody.split("&"); |
| 114 |
|
| 115 |
attrs = urlEncodedParts.reduce((a, urlEncodedPart) => { |
| 116 |
let [key, value] = urlEncodedPart.split("="); |
| 117 |
a[key] = decodeURIComponent(value.replace(/\+/g, " ")); |
| 118 |
return a; |
| 119 |
}, {}); |
| 120 |
|
| 121 |
return attrs; |
| 122 |
} |
| 123 |
} |