Members
QueryString
An object with default parse
and stringify
functions from qs
addRelatedRecord
Adds a record to a related array and updates the jsonapi reference in the relationships
- Source
arrayType
Strips observers and returns a plain JS array
coerceDataToExistingRecord
Takes any object with { id, type } properties and gets an object from the store with that structure. Useful for allowing objects to be serialized in real time, saving overhead, while at the same time always returning an object of the same type.
- Source
dateType
Coerces a string or date to a date
defineToManyRelationships
Takes the toMany
definitions from a document type and creates getters and setters. A getter finds records from the store, falling back to a lookup of the inverse records if none are defined in the relationships
hash.
The setter will unset the previous inverse and set the current inverse. Both return a RelatedRecordsArray
, which is an array with added methods add
, remove
, and replace
A definition will look like this:
categories: { direction: 'toMany', inverse: { name: 'organization', direction: 'toOne' } }
- Source
defineToOneRelationships
Takes the toOne
definitions from a document type and creates getters and setters. A getter finds a record from the store. The setter calls setRelatedRecord
, which will return an instance of a model and add it to the inverse relationship if necessary. A definition will look something like this:
todo: { direction: 'toOne', inverse: { name: 'notes', direction: 'toMany' } }
- Source
definitionsByDirection
Gets only the relationships from one direction, ie 'toOne' or 'toMany'
- Source
isEmptyString
Returns true if the value is an empty string
mobxAnnotations
Annotations for mobx observability. We can't use makeAutoObservable
because we have subclasses.
mobxAnnotations
Annotations for mobx observability. We can't use makeAutoObservable
because we have subclasses.
numberType
Coerces a value to a number
objectType
Strips observers and returns a plain JS object
removeRelatedRecord
Removes a record from an array of related records, removing both the object and the reference.
- Source
serverResponse
Encodes models into full compliant JSONAPI payload, as if it were being sent with all relevant relationships and inclusions. The resulting payload will look like { data: { id: '1', type: 'zones', attributes: {}, relationships: {}, }, included: [] }
- Source
setRelatedRecord
Sets a related record, as well as the inverse. Can also remove the record from a relationship.
- Source
stringType
Coerces a value to a string
validatesArray
Returns valid if the value is an array
validatesArrayPresence
Is valid if the array has at least one object
validatesOptions
Valid if target options are not blank
validatesString
Is valid if the value is not an empty string
Methods
addIncluded(store, encodedModel, included, allEncoded)
JSONAPI uses included
only at the top level. To recursively add models to this array, we preserve the top-level object and pass it in to the next round Because objects can have multiple relationships, we do a check of the array to make sure it's not already there.
Name | Type | Description |
---|---|---|
store | object | the data store |
encodedModel | object | the the model |
included | Array | data |
allEncoded | Array | the previously encoded models |
- Source
circularFetchError(url, options)
Will throw an error if fetch
is called from the mockServer, usually due to a POST
or PATCH
called by a save
Name | Type | Description |
---|---|---|
url | string | the url that is attempted |
options | object | options including the http method |
- Source
circularFindError(type, id)
Throws an error if MockServer tries to findOne
or findAll
from itself.
Name | Type | Description |
---|---|---|
type | string | the model type |
id | string | the model id |
- Source
combineRacedRequests(key, fn) → {Promise}
Avoids making racing requests by blocking a request if an identical one is already in-flight. Blocked requests will be resolved when the initial request resolves by cloning the response.
Name | Type | Description |
---|---|---|
key | string | the unique key for the request |
fn | function | the function the generates the promise |
the request
- Type:
- Promise
decrementor(key) → {number}
Decreases a counter by 1
Name | Type | Description |
---|---|---|
key | string | the counter to decreases |
the current count
- Type:
- number
deriveIdQueryStrings(ids, restOfUrl) → {Array.<string>}
Splits an array of ids into a series of strings that can be used to form queries that conform to a max length of URL_MAX_LENGTH. This is to prevent 414 errors.
Name | Type | Description |
---|---|---|
ids | Array | an array of ids that will be used in the string |
restOfUrl | string | the additional text URL that will be passed to the server |
an array of strings of ids
- Type:
- Array.<string>
diff(a, b) → {Array.<string>}
deeply compare objects a and b and return object paths for attributes which differ. it is important to note that this comparison is biased toward object a. object a is walked and compared against values in object b. if a property exists in object b, but not in object a, it will not be counted as a difference.
Name | Type | Description |
---|---|---|
a | object | the first object |
b | object | the second object |
the path to differences
- Type:
- Array.<string>
disallowFetches(store)
Overrides store methods that could trigger a fetch
to throw errors. MockServer should only provide data for fetches, never call a fetch itself.
Name | Type | Description |
---|---|---|
store | object | the internal store |
- Source
fetchWithRetry(url, fetchOptions, attempts, delay) → {Promise}
Implements a retry in case a fetch fails
Name | Type | Description |
---|---|---|
url | string | the request url |
fetchOptions | object | headers etc to use for the request |
attempts | number | number of attempts to try |
delay | number | time between attempts |
the fetch
- Type:
- Promise
getOneFromFactory(_backendFactoryFarm, factory, type, id) → {object}
Finds or creates a model that will match an id. This is useful for creating a response on the fly if no object already exists
Name | Type | Description |
---|---|---|
_backendFactoryFarm | object | the private factory farm |
factory | object | the the factory to use |
type | string | the model type |
id | string | the id to find |
- Source
a Model object
- Type:
- object
incrementor(key) → {number}
Increments a counter by 1
Name | Type | Description |
---|---|---|
key | string | the counter to increment |
the current count
- Type:
- number
isValid(value) → {boolean}
Returns true
if the value is truthy
Name | Type | Description |
---|---|---|
value | any | the value to check |
true if the value is present
- Type:
- boolean
makeDate(value) → {Date}
convert a value into a date, pass Date or Moment instances thru untouched
Name | Type | Description |
---|---|---|
value | Date | | a date-like object |
a date object
- Type:
- Date
newId() → {string}
Generates a temporary id to be used for reference in the store
a uuidv1 string prefixed with tmp
- Type:
- string
parseErrorPointer(error) → {object}
Parses the pointer of the error to retrieve the index of the record the error belongs to and the full path to the attribute which will serve as the key for the error.
If there is no parsed index, then assume the payload was for a single record and default to 0.
ex. error = { detail: "Foo can't be blank", source: { pointer: '/data/1/attributes/options/foo' }, title: 'Invalid foo' }
parsePointer(error)
{ index: 1, key: 'options.foo' }
Name | Type | Description |
---|---|---|
error | object | the error object to parse |
the matching parts of the pointer
- Type:
- object
parseErrors(response, errorMessages) → {Array.<object>}
Parses JSONAPI error objects from a fetch response. If the response's body is undefined or is not formatted with a top-level errors
key containing an array of errors, it builds a JSONAPI error object from the response status and a errorMessages
configuration.
Errors that are returned which contain a status also have their detail
overridden with values from this configuration.
Name | Type | Description |
---|---|---|
response | object | a fetch response |
errorMessages | object | store configuration of error messages corresponding to HTTP status codes |
An array of JSONAPI errors
- Type:
- Array.<object>
requestUrl(baseUrl, endpoint, queryParams, id) → {string}
Build request url from base url, endpoint, query params, and ids.
Name | Type | Description |
---|---|---|
baseUrl | string | the base url |
endpoint | string | the endpoint of the url |
queryParams | object | query params to add |
id | string | the id of the the model |
formatted url string
- Type:
- string
simulatePatch(store, type, body) → {object|Array}
Interpret a PATCH
request
Name | Type | Description |
---|---|---|
store | object | the store |
type | string | the type |
body | string | json encoded response body |
- Source
a model or array created from the response
- Type:
- object |
Array
simulatePost(store, type, body) → {object|Array}
Interpret a POST
request
Name | Type | Description |
---|---|---|
store | object | the store |
type | string | the type |
body | string | json encoded response body |
- Source
a model or array created from the response
- Type:
- object |
Array
stringifyIds(object)
Coerces all ids to strings
Name | Type | Description |
---|---|---|
object | object | object to coerce |
toFullJsonapi(model) → {object}
Encodes a model to a jsonapi document with all relationships
Name | Type | Description |
---|---|---|
model | object | the model to convert |
- Source
the jsonapi encoded document
- Type:
- object
validatePresence() → {object}
returns true
as long as the value
is not null
, undefined
, or ''
a validation object
- Type:
- object
validateProperties(model, propertyNames, propertyDefinitions) → {Array}
Maps the passed-in property names through and runs validations against those properties
Name | Type | Description |
---|---|---|
model | object | the model to check |
propertyNames | Array | the names of the model properties to check |
propertyDefinitions | object | a hash map containing validators by property |
an array of booleans representing results of validations
- Type:
- Array
walk(obj, iteratee, prefix) → {Array}
recursively walk an object and call the iteratee
function for each property. returns an array of results of calls to the iteratee.
Name | Type | Description |
---|---|---|
obj | object | the object to analyze |
iteratee | function | the iterator to use |
prefix | string | the prefix |
the result of iteratee calls
- Type:
- Array
wrapResponse(response, method, status) → {Promise}
Wraps response JSON or object in a Response object that is itself wrapped in a resolved Promise. If no status is given then it will fill in a default based on the method.
Name | Type | Description |
---|---|---|
response | string | JSON string |
method | string | the http method |
status | number | the http status |
- Source
a promise wrapping the response
- Type:
- Promise