Node.js integration layer
npm install integreatAn integration layer written in TypeScript.


The basic idea of Integreat is to make it easy to define how to send data to
and receive data from a set of services, and expose them
through a well defined interface, abstracting away the specifics of each
service.
There are a few concepts that makes this possible:
- Transporters and adapters speak the
language of different types of services and standards of data exchange, and
does the basic translation to and from the structures used by Integreat. You
deal with familiar JavasScript objects, arrays, and primitive data types,
regardless of what the service expects.
- Mutation pipelines let you define how the data coming from
or going to a service should be transformed. This includes changing the
overal structure, renaming properties, transforming and filtering values with
transformer functions, etc. You may also provide your own transformer
functions.
- Schemas serve as a common normalization of data between
services. You define your own schemas and mutate data to and from them,
enabling inter-service sharing of data. If you have data in one schema, you
may send it to any service where you have set up the right mutations for this
schema, again abstracting away all service details.
All configuration is done through basic JSON-friendly structures, and you
define your services with different endpoints, mutation pipelines,
authentication schemes, etc.
Your configuration is spun up as an Integreat instance. To send and retrieve
data, you dispatch actions to your instance and get
response objects back. You may define jobs to
run simple actions or longer "flows" consisting of several actions with
conditions and logic. You may also configure queues to have actions
run in sequence or on a later time.
```
____________________________________________________
| |
| Integreat instance |
Action ----| | |
|-> Dispatch <-> Schema <-> Mutation <-> Adapter <-> Transporter <-> Service
Response <-| | |
|___________________________________________________|
To deal with security and permissions, Integreat has a concept of an ident.
Other authentication schemes may be mapped to Integreat's ident scheme, to
provide data security from a service to another service or to the dispatched
action. A ground principle is that nothing that enters Integreat from an
authenticated service, will leave Integreat unauthenticated. What this means,
though, depends on how you define your services.
1. Usage
1. Install
2. Basic example
2. Integreat concepts
1. Services
2. Transporters
3. Adapters
4. Authenticators
5. Mutations
6. Schemas
7. Actions
8. Jobs
9. Queues
10. Middleware
3. Debugging
Requires node v18.
Note: This package is native ESM. See this
guide on how to convert to or use ESM packages.
Install from npm:
``
npm install integreat
You will probably also need some transporters and
adapters, and the basic transformers in
integreat-transformers.
The following is the "hello world" example of Integreat. As most hello world
examples, this is a bit too trivial a use case to demonstrate the real
usefulness of Integreat, but it shows you the simplest setup possible.
Here, we fetch cat facts from the API endpoint
'https://cat-fact.herokuapp.com/facts', which returns data in JSON and requires
no authentication. The returned list of facts are mutated and cast to the
fact schema. We only fetch data _from_ the service, and no data is sent _to_
it.
`javascript
import Integreat from 'integreat'
import httpTransporter from 'integreat-transporter-http'
import jsonAdapter from 'integreat-adapter-json'
const schemas = [
{
id: 'fact', // The id of the schema
shape: {
// The fields of the type
id: 'string', // An id field will always be included, but we define it here for readability
text: 'string', // The text of the cat fact
createdAt: 'date', // The created date (createdAt and updatedAt will always be dates)
},
access: { allow: 'all' }, // No access restrictions
},
]
const services = [
{
id: 'catfact', // The id of the service
transporter: 'http', // Use the http transporter
adapters: ['json'], // Run the request and the response through the json adapter
options: {
transporter: {
// Options for the transporter
uri: 'https://cat-fact.herokuapp.com/facts', // Only the uri is needed here
},
},
endpoints: [
{
match: { action: 'GET', type: 'fact' }, // Match to a GET action for type 'fact'
mutation: {
$direction: 'from', // We're mutating data _from_ the service
// Here we're mutating response.data and "setting it back" where we found it ..._id
'response.data': [
'response.data[]',
{
$iterate: true, // Mutate each item in an array
id: '_id', // The id is called the data from the servicetext
text: 'text', // text is called createdAt
createdAt: 'createdAt', // Creation date is called
},
],
},
},
],
},
]
// Create the Integreat instance from our definitions and provide the
// transporters and adapters we require.
const great = Integreat.create(
{ schemas, services },
{ transporters: { http: httpTransporter }, adapters: { json: jsonAdapter } },
)
// Prepare an action to fetch all cat facts from the service catfact
const action = { type: 'GET', payload: { type: 'fact', service: 'catfact' } }
// Dispatch the action and get the response
const response = await great.dispatch(action)
`
The response object will look like this:
`javascript`
{
status: 'ok',
data: [
{
id: '58e008780aac31001185ed05',
$type: 'fact',
text: 'Owning a cat can reduce the risk of stroke and heart attack by a third.',
createdAt: new Date('2018-03-29T20:20:03.844Z')
},
// ...
]
}
As mentioned in the introduction, the building blocks of Integreat are
services, transporters and adapters, mutation pipelines, and schemas.
A service is the API, database, FTP server, queue, etc. that you want to get
data from and/or set data to. We pass on a set of service definitions to
Integreat, specifying what transporter, adapters, authentication schemas it
requires, in adition to defining the different endpoints available on the
service, how they should be called, and how data should be mutated in each
case.
We'll get back to the details of all of this in turn, but first we want to
highlight how central the concept of a service is to Integreat. Basically, in
Integreat "everything is a service". A simple REST/JSON API is a service, a
database is a service, and everything external you want to communicate with are
services. Want to set up a queue to handle actions one by one? That's a
service. Want to cache data in a memory store? That's a service. Want to
schedule actions to run on intervals? That's a service to. By simply defining
services and their specifics, you may set up a variety of different types of
configurations with the same few building blocks. This is very powerful as
soon as you get into the right mindset.
Services are configured by service definitions, and tells Integreat how to
fetch data from a service, how to mutate this data to schemas, and how to send
data back to the service.
The service definition object includes the transporter id, adapter ids, any
authentication method, the endpoints for fetching from and sending to the
service, mutations that data to all endpoints will pass through, and options
for transporters, adapters, etc.
`javascript`
{
id:
transporter:
adapters: [
auth:
meta:
options: {...},
mutation:
endpoints: [
...
]
}
Service definitions are passed to Integreat on creation through the
Integreat.create() function. There is no way to change service defintions
after creation.
See mutations for a description of how to define the mutation
pipeline for a service.
The auth property should normally be set to the id of anauth
auth definition, if the service requires
authentication. In cases where the service is authenticated by other means,
e.g. by including username and password in the uri, set the property totrue to signal that this is an authenticated service. For services acceptingauth
incoming actions, should be set to an object with{ outgoing: . To accept several
incoming actions, provide an array of , and they will be runnoaccess
from first to last until one of them returns an ident or an error other than.
> [!NOTE]
> When connecting to a service for listening, the outgoing auth isincoming
> used. is only used for validating the actions being dispatched
> "back" from the service.
In options, you may provide options for transporters and adapters. It isoptions
merged with the object on the endpoint. Seeoptions
the object for more on this.
A service will have at least one endpoint, but often there will be several.
Endpoints are the definitions of the different ways Integreat may interact with
a service. You decide how you want to set up the endpoints and what is the
right "endpoint design" for a service, but there might be one endpoint for each
operation that can be done with a type of data.
For example, let's say you have a simple REST API with blog articles and
authors. There will most likely be an endpoint to fetch all (or some) articles,
one endpoint for fetching one article by id, one endpoint for creating an
article, one for updating an article, and so on. And you'll have similar
endpoints for authors, one endpoint for fetching all, one for fetching one by
id, one endpoint for creating an author, etc. As this is REST, each endpoint
will address a different combination of urls and http verbs (through the
transporter).
As another example, you may be accessing a database of articles and authors
directly. The configuration details will be very different than for a REST API,
but you'll probably have the same endpoints, fetching all articles, fetching
one, creating, updating, and the same all over for users. Instead of urls and
http verbs, as for REST, these endpoints will address different databases and
different database operations (through the transporter).
> [!NOTE]
> This is not to say that Integreat requires you to set up endpoints
> exactly as described in these examples, it might be that you would like to
> set up an endpoint that handles many of these cases. The intention here is
> just to give you an understanding of what an endpoint is in Integreat.
When you dispatch an action, Integreat will figure out what service and what
endpoint to send the action to. The target service is often specified in the
action payload with the targetService (or shorthand service) property, buttype
if not, the default service of the schema specified with the payload
property, will be used.
The targetService property will be set on the the action payload when it istargetService
sent to the transporter, before it goes through the middleware. There are two
exceptions to this, however. will not be set for a queuedmeta.options.doSetTargetService
action (and it will not be removed if it is already set), and it will not be
set for actions where the flag is set tofalse.
The matching to an endpoint is done by finding the endpoint whose match
object matches the action with most accuracy. The rules of the endpoint
matching is describe in more details below.
Here's the format of an endpoint definition:
`javascript`
{
id:
match: {
type:
scope: <'collection'|'member'|'members'|'all'>,
action:
params: {...},
incoming:
conditions: [...]
},
validate: [
{
condition:
failResponse:
}
],
mutate:
adapters: [
auth:
allowRawRequest:
allowRawResponse:
castWithoutDefaults:
options: {...},
}
All of these properties are optional. An empty endpoint defintion object will
match anything, pass on the action to the transporter untouched, and relay any
response coming back. This might be what you need, but often you'll want to
specify a few things:
- id: The endpoint may have an id, which you may use to specify that you wantmatch
an action to go to this particular id. However, most of the time you'll set
up the object so that Integreat will decide what endpoint to use formatch
the action you dispatch.
- : The match object is used to decide the right endpoint for an action.validate
More one this in the Match properties section.
- : This is an array of condition that have to be met in order forcondition
Integreat to proceed with the endpoint. The is a mutationvalidate
pipeline that should return a truthy value for the validation to pass. Any
falsy value will cause the validation to fail. If is missing ormatch
an empty array, no validation will be done. This may sound similar to
, but validate is only processed after a match is found, and if thefailResponse
validation fails, no other endpoint is considered. On a failing validation,
the is returned as the response from this action, or abadrequest
response if no failResponse is provided. There's also afailResponse
shorthand, where you set to a string, which will be theerror
message of the badrequest response. The response is passed throughmutate
the mutation pipeline.
- : A mutation pipeline for the endpoint. The pipeline is run for bothmutation
actions going to a service and the response coming back, so keep this in mind
when you set up this pipeline. See Mutation pipelines
for more on how to define the mutation. is an alias for mutate.adapters
- : An array of adapter ids that will be appended to the array ofauth
adapters set on the service.
- : Auth config that will override the auth config on the service. Seeauth
description of under Services for more on this. Theauth
endpoint will only apply in cases where we have an endpoint, like whenauth
we're sending a request to a service or receiving an incoming request, but
when we're e.g. connecting to a service to start listening, the on theallowRawRequest
service will be used. This also goes for incoming requests where the
transporter does not provide an action with the auth attempt.
- : When set to true, payload data sent to this endpointallowRawResponse
will not by cast automatically nor will an error be returned if the data is
not typed.
- : When set to true, response data coming from thisfalse
endpoint will not be cast automatically nor will an error be returned if the
data is not typed. The default is , expcept for incoming endpointsmatch
(endpoints where object has incoming: true) where the default valuetrue
is .castWithoutDefaults
- : Set to true when you don't want to set defaultid
values on casted data. This also means no will be generated and nocreatedAt
or updatedAt will be set – when any of these are missing infalse
the data. Default is .options
- : This object is merged with the options object on the serviceoptions
definition, and provide options for transporters and adapters. See
the object for more on this.
#### Match properties
An endpoint may specify none or more of the following match properties:
- type: When set, the endpoint will only be used for actions with thetype
specified schema type (the schema's id). may also be an array ofscope
types, matching any one of the schemas in the list.
- : May be member, members, collection, or all, to specify thatmember
the endpoint should be used to request one item (member) by id, several items
by ids (members), or an entire collection of items. Setting this to members
or will only match actions with a payload id property, and theid
should be an array of ids for members. Not setting this property, orall
setting it to , signals an endpoint that will work for all scopes.action
- : May be set to the type string of an action. The endpoint will matchaction
only actions of this type. When this is not specified, any action type will
match. may also be a list of action types, matching any of these.params
- : This object should list all params that this endpoint supports. Atype
param in this context is any property on the action payload except ,id
, or data. Use the param name as key on this object and set the valuetrue
to if it is required, and false if it is optional. When matchingincoming
endpoints, an action will only match if it has all the required params, and
in case several match, the endpoint with more specified params will be
preferred.
- : If this is true, it will only match incoming actions, iffalse
only outgoing, and if not set, it will match both.conditions
- : An array of mutation pipelines that will be run on the actionfalse
to see if it's a fit for this endpoint. If all pipelines return a truthy
value, the endpoint is chosen (given that the other match properties also
match). We rely on JavaScript definition of 'truthy' here, so any value that
is not , null, undefined, 0, NaN, or '' will be considered
truthy.
> [!NOTE]
> There used to be a filters property on the endpoint match object. It isconditions
> still supported, but it's deprecated and will be removed in v1.1. Please use
> instead.
> [!NOTE]
> Editor's note: Describe what incoming actions are, and give more details on
> filters.
There might be cases where several endpoints match an action, and in these
cases the endpoint with the highest level of specificity will be used. E.g.,
for a GET action asking for resources of type entry, an endpoint with bothaction: 'GET' and type: 'entry' is picked over an endpoint matching allGET actions regardless of type. For params and filters this is decided by
the highest number of properties on these objects.
The order of the endpoints in the endpoints list matters only when two
endpoints are equally specified with the same match properties specified. Then
the first one is used.
When no match properties are set, the endpoint will match any actions, as long
as no other endpoints match.
Finally, if an action specifies the endpoint id with the endpoint
payload property, this overrides all else, and the
endpoint with the id is used regardless of how the match object would apply.
Example service definition with endpoint match object:
`javascript`
{
id: 'entries',
transporter: 'http',
endpoints: [
{
match: {
type: 'entry',
action: 'GET',
scope: 'collection',
params: {
author: true,
archive: false
}
},
options: {
transporter: {
uri: 'https://example.api.com/1.0/{author}/{type}_log?archive={archive}'
}
}
}
],
// ...
}
A service defintion may have options object in two places: Direction on theoptions
service definition and on any of the endpoints. When an action is sent to an
endpoint, the combination of the two are used. Also, there may be
different options for the transporter and for the adapters.
Example of an options object set on the service definition:
`javascript`
{
id: 'entries',
options: {
uri: 'https://ourapi.com/v1',
transporter: {
method: 'POST',
incoming: { port: 3000 }
},
adapters: {
xml: { namespaces: { ... } },
// ...
}
}
}
Any properties set directly on the options object or on a transporteroptions
property, are treated as options for the transporter. If there are properties
on both the and a transporter object, they will be merged, with thetransporter object having priority if conflicts. This is a shallow merge, so
objects used in the options will not be merged.
In the example above, the options passed to the transporter will include uri,method, and incoming.
The incoming object on the transporter options is a bit special, as it holdslisten()
separate options for transporters that support incoming requests trough the method. If there are incoming objects on both the options andtransporter objects, they will be merged, again with priority to the one ontransporter
the object.
Note that we recommend setting transporter options on the transporter object
for clarity, but both will work.
Adapter options may be given in an adapters object, where each adapter mayxml
have its own options, set with the id of the adapter as a key. In the example
above, the adapter will be given the namespaces object. A requirement
for this, is that the adapter actually have an id. Adapters provided directly
on service definition may not have an id, but all adapters that are referenced
by an id, will also be given options set on that id, which is the common behavior.
Finally, when all this sorting have been done on options from both the service
definition and an endpoint, the two options structures are merged before being
used. Here, the endpoint options take priority, so that you may set a general
option on the service, and override it on the endpoint.
Example of endpoint options overriding service options:
`javascript`
{
id: 'entries',
options: {
transporter: {
uri: 'https://ourapi.com/v1',
method: 'GET',
}
},
endpoints: [
{
match: { ... }
},
{
match: { ... },
options: {
transporter: {
method: 'POST'
}
}
}
]
}
Here, the first enpoint will be given method: 'GET', while the next will getmethod: 'POST'.
Before actions are passed through mutations and finally passed to the
transporter, the merged transporter options is set on an options property inmeta
the object of the action. This way, you may also mutate these options
before they reach the transporter.
This definition format is used to authenticate with a service:
`javascript`
{
id:
authenticator:
options: {
// ...
},
overrideAuthAsMethod:
}
- id: The id used to reference this authentication, especially from theauthenticator
service definition.
- : The id of an authenticator used tooptions
authenticate requests. Integreat comes with a few basic ones built in, and
there are others available.
- : An object of values meaningful to the authenticator. See theoverrideAuthAsMethod
documentation of each authenticator to learn how it should be configured.
- : Transporters specify a default method for getting anasHttpHeaders
auth object that makes sense for authenticating with the service. For
instance, the HTTP transporter has as the default, to get theoverrideAuthAsMethod
relevant auth headers to send with the request. With ,undefined
you may override this in the service auth definition when relevant. Default
value is , meaning "no override". Note that we say "method" here,
but the value is a string with the name of the auth-as method to use.
The authenticator is responsible for doing all the heavy-lifting, based on the
options provided in the service authentication definition.
Integreat supports getting and setting metadata for a service. The most common
use of this is to keep track of when data of a certain type was last synced.
Some services may have support for storing their own metadata, but usually you
set up a dedicated service for storing other services' metadata. A few
different pieces goes into setting up a meta store:
- A meta schema with the fields available as metadata
- A service for storing metadata, with an endpoint suporting the metadata
schema
- Possible a metadata mutation for the metadata service
When all of this is set up, you activate the metadata on the service the
metadata will be stored for, by setting the meta property to the id of theservice
schema defining the metadata fields. The set on the schema will tell
Integreat what service to get and set the metadata from/to.
The schema will look something like this:
`javascriptmeta
{
id: 'meta', // You may give it any id you'd like and reference it on the prop on the service`
service:
shape: {
// ...
}
}
To get or set metadata, use GET_META and SET_META
with the service you are getting metadata from as the service. Integreat will
figure out the rest.
A transporter handles all the details of sending and receiving data to and from
a service. When dispatching an action to a service, the action will be handled
in a relevant manner for the type of service the transporter supports, e.g.
sending an http requrest for the HTTP transporter, or doing a query to a
database for the MongoDb transporter. Some transporters may also support
listening to a service, e.g. the HTTP transporter listing for incoming requests
or the MQTT transporter subscribing to events on a topic.
Integreat has transporters for some common cases, and more may come:
- Bull
- FTP
- HTTP
- MongoDb
- MQTT
- Redis
You may write your own transporters if your case is not covered by any of
these. Documentation on developing transporters are coming.
Integreat will handle the transporters based on you configurations, but there
are some specifics to each transporter, like HTTP needing an uri option orcollection
MongoDb needing a option. See the documentation of each
transporter for more.
Adapters are working together with transporters to prepare the incoming and
outgoing data in accordance with the type of services they support.
As an example, the HTTP transporter will return data from a response as a
string, since there is no common way to treat the response body. So for a JSON
API, you will configure the JSON adapter to make sure the data from the
mutations are sent as a JSON string, and that the JSON comming back from the
service is parsed before mutation starts. For a service using XML, you would
instead set up the XML adapter, and perhaps also the SOAP adapter, to again
stringify and parse the data going back and forth.
The MongoDb transporter, on the other hand, does not require any adapters, as
documents from the database will always come as arrays and object, and may be
fed directly into the mutation pipelines.
Integreat currently have the following adapters:
- CSV
- JSON
- SOAP
- Url encoded form data
- URI templates
- XML
You may write your own adapters as well, and documentation on this is coming.
At its simplest, an authenticator will provide necessary credientials to an
outgoing action, or an ident to an incoming action. Some authenticators do this
based only on the options provided, while others will do a more complex dance
with the service or a third-party service, like with OAuth2.
When setting up a service, you may provide it with an auth id that
refers to a service authentication definition, that
again refers to an authenticator by id. The service auth definition also holds
options for the authenticator, so when assigning an auth id to a service,
you're assigning it an authenticator with those specific options. Another
service may use the same authenticator, but with different options, and you
would set this up with a different service authentication definition.
Authentication for outgoing actions are done when sending the action. When
authenticated, an auth object is retrieved with the auth-as method specified on
the transporter (e.g. asHttpHeaders for the http transporter), or on theoverrideAuthAsMethod in auth options if set. Themeta.auth
auth object is passed to the transporter on the action prop. It isauthInData
applied just before sending it, though, so it will be available to service
middleware, but not to the mutation pipeline. This is done to expose
credentials in as few places as possible. If you however _want_ to have the
auth object in mutations, set to true on the service orpreflightAction
endpoint options, and authentication will be done in the meta.auth
step instead, making it available on throughout the entire mutation pipeline.
For incoming actions, authentication is done when a listening action calls the
authenticate() callback. The validate() method on the authenticator is used
here, which will provide the transporter with an authorized ident.
Available authenticators:
- action: Will dispatch an action and use the response data to create anoptions
authentication. The should have the action type as action, andpayload
the entire payload as . The response data should have an authexpire
object, that will be used directly, and an optional that is aexpire
timestamp on which the auth will expire, in milleseconds since Epoc
(1970-01-01). If no is returned, the expireIn option will be usedms
if present. It is given as milliseconds before the auth should expire, or a
string like '1h'.http
- : Supports http native authentications, like Basic and Bearer. It'sident
included with the
HTTP transporter.
- : Will always grant access and validate() will return an ident withidentId
the id provided in on the options object, or 'anonymous' if noidentId
is provided. This is built into Integreat.options
- : Will pass on the options as authentication, so whatever youidentId
provide here is the authentication. What options to provide, then, is
depending on what the relevant transporter requires. For outgoing actions,
the options are provided as is. Incoming action are validated agains the
values given in the options (the keys may be dot notation paths in this
case, and is excluded). An ident with the identId from theid
options as , is returned if the action matches. This is built intotoken
Integreat.
- : A simple way of authenticating with a given token. For HTTPAuthorization
requests, the token will be provided as a header, and aBasic
configurable prefix like or Bearer. This is built into Integreat.jwt
- : Willoauth2
generate and encode a JavaScript Web Token (JWT) based on the options.
- :
Will run the balett of calling different OAuth2 endpoints and receive a token
based on the provided options.
Both on the service and on endpoints, you define mutation pipelines. The
service mutation is run before the endpoint mutation for data coming from a
service, and in the oposite order when going to a service.
A nice - but sometimes complicated - thing about mutations, is that they are
run in both directions. They are by default defined for mutating data coming
_from_ a service, and will be run in reverse for data going _to_ a service. In
some cases this reversing of the pipeline will work as expected without
modifications -- you define the mutation pipeline for data coming _from_ the
service, and the reversed pipeline works _to_ as well. But many times you need
to make adjustments and sometimes you'll have to have separate steps based on
the direction. We'll get into more details in the following.
A mutation pipeline consists of one or more steps that the data will go
through, before coming out on the other in the desired shape. It helps
picturing this as an actual pipeline. After each step, data will be in a
different shape, and this is the input to the next step.
You define a pipeline in Integreat with an array, although for a pipeline with
only one step, you may skip the array for simplicity.
Each step may be one of the following:
- A dot notation path, e.g. path.to.data. The data$iterate: true
at that path will be extracted, and will be provided as the data to the next
step in the pipeline. When going in reverse, the data will be set on that
path instead.
- A mutation object is an object that basically describes the object you
want as a result, where the keys are dot notation paths and the values are
mutation pipelines. Each pipeline on the mutation object will be run on the
data, and then set on the path, resulting in an object that will be passed on
to the next step. Setting on the object will cause it to$modify: true
iterate over items in an array, otherwise it will be applied to the array.
Setting will cause any properties on an object in the$modify
pipeline not set in the mutation, to be included, much like the spread in
JavaScript. Setting to a path works the same, but you will spread$modify: true
from the object at the path ( is equal to $modify: '.').{ $transform: 'number' }
- A transform object letting you run a transformer function on the data,
e.g. to transform the value into a number, orundefined
if not possible.{ $filter: 'boolean' }
- A filter object that will run a transformer function on the data and
filter away any items not resulting in a truthy value. As an example,
will filter away anything that is not convertable totrue
in JS rules. When applied to an array, you'll get an array where itemsundefined
are filtered away. For an object or a plain value, filtering away will means
is passed on to the next step in the pipeline.then
- An if object that runs a pipeline if the provided pipeline returnselse
truthy, and the pipeline if it returns falsy.{ $cast: 'author' }
- A cast object, e.g. that casts the data into aundefined
schema, removing all properties that is not part of the shape of the schema,
and transforming all values to the expected types or if not
possible. (Not available until v1.0)
At its most basic, a dot notation path is just a property key, like content.content.articles
You may dive into a data structure by adding a key from the next level,
separated by a dot, like . With an object like this:
`javascript`
{
content: {
articles: [{ id: '1' }, { id: '2' }],
authors: [{ id: 'john' }]
}
}
... the path content.articles will give you the array[{ id: '1' }, { id: '2' }].
You may add brackets to the path to traverse into arrays, e.g.
content.articles[0] will give you the object { id: '1' }, andcontent.articles[0].id will give you '1'.
Empty brackets, like content.articles[] will ensure that you get an arraycontent.articles
back. If the data at the path is an array, this will return the same as, but if the path returns an object or a plain value, it will
be returned in an array.
When mapping data _to_ a service, the paths are used to reconstruct the data
format the service expects. Only properties included in the paths will be
created.
Arrays are reconstructed with any object or value at the first index, unless a
single, non-negative index is specified in the path.
You may use a carret ^ to go one level up -- to the parent -- in the datacontent.articles
(after going down), so after , the path ^.authors will[{ id: 'john' }]
return . Arrays count as one level, so aftercontent.articles[0] you will need to go up twice like so: ^.^.authors.
A double carret ^^ takes you to the top -- the root -- so aftercontent.articles[0].id, ^^.content.authors returns [{ id: 'john' }].
Carret notations -- parents and roots -- does not currently work in reverse,
but they might in a future version.
The behavior of some transformers are based upon certain values being
non-values. E.g. { $alt: [ will use the valuenull
from the first pipeline if it returns a value, otherwise the value from the
second pipeline, meaning it will check for non-values. By default ,undefined, and '' (empty string) are non-values. By setting the nonvaluesIntegreat.create()
param to an array of values in the defintions object you pass to, you may specify your own non-values.
If you don't want empty string to a non-value, for instance, you do this:
`javascript`
const great = Integreat.create({
nonvalues: [null, undefined],
// ... other definitions
})
A central idea to Integreat, is that any integration has two sides; the getting
of data from one service and the sending of data to another. Instead of setting
up an integration directly from A to B, you have a schema in middle, and
configure how data from A will be mutated to a schema, and then have data in
that schema will be mutated and sent to B.
This is a useful abstraction, and if you ever need to change one side, you can
do so without involving the other side. If you need to switch out service B
with service C, you can do so without involving the configuration of service
A, or you can send data to both B and C, using the same setup for service A.
To be clear, you can setup flows without schemas in Integreat, but then you may
loose this flexibility and maintainability.
A schema describe the data you expected to get out of Integreat, or send
through it. You basically define the fields and their types, and may then cast
data to that shape. Note that data on an action for a specified type, will be
automatically cast to that type.
`javascript`
{
id:
plural:
service:
shape: {
$type:
default:
const:
},
},
access:
}
- id: The id of the schema, used to reference it in actions (the payloadtype
), when casting to the schema with { $type: ', and to$type
signal what schema a data object is cast to (the prop on typed dataid
items). The convention is to use singular mode for the , e.g. if your'article'
defining a schema for articles, you would give it the id .plural
- : When the plural of id is not simply a matter of adding an 's',id: 'entry'
you may specify the plural mode here. E.g. would haveplural: 'entries'
. This is not used by Integreat right now, but it may beservice
used in the future for error messages, generating APIs from schemas, etc.
- : You may specify a default service for the schema when it makes{ type: 'GET', payload: { type: 'article' } }
sense. This allows you to dispatch an action for a type without specifying
the target service, e.g. ,service
and have Integreat use the default service. This is a way of hiding
configuration details from the code dispatching the actions, and you may
also change the default service without changing the dispatching code if
need be. You may always override this by specifying a on theshape
action payload.
- : This is where you define all the fields, seegenerateId
the section below.
- : Set this to true to generate a unique id for the id fieldid
when the data being cast does not provide an . Default is false, whichid: null
will just set . The id will be 36 chars and consist of A-Z, a-z,'_'
0-9, underscore , and hyphen '-'.access
- : Integreat lets you define authorization schemes per schema. All useaccess
of data cast to a schema will then be controlled by the rules you set here.
See Access rules below for details on these rules. Note that
is optional, but when you get data from a service where any form ofaccess
authentication is used to access the data, you will not be able to do
anything with the data unless you cast it to a schema with set up
(or specifically says that you allow raw data from that endpoint).
The shape is defined by an object where each key is the id of a field, which
may contain only alphanumeric characters, and may not start with a digit. A
schema cannot have the same id as a primitive type (see list below).
The values on this object define the types of the fields and a few other
optional features:
`javascript`
{
$type:
default:
const:
}
The $type prop sets the type of the field. The available primitive types, arestring, integer, float (or number), boolean, and date. A field may$type
also have another schema as its type, in which case the id of the schema is set
in . An example can be anarticle schema with an author field of type user, referring to a schemauser
with id . When casting the article, data on the author prop will beuser
cast with the schema.
The default value will be used when the field is undefined, null, or notdefault
preset in data object being cast to this schema. If is set to adefault
function, the function will be run with no argument, and the returned value is
used as the default value. When no is given, undefined is used.
The const value override any value you provide to the field. It may be usefuldefault
if you want a field to always have a fixed value. Just as for , you
may set it to a function, in which case the function will be run without
arguments and the returned value will be used.
If both const and default are set, const will be used.
When only setting the field type, you don't need to provide the entire object,
you can just provide the type string.
Example schema:
`javascript`
{
id: 'article',
shape: {
id: 'string', // Not needed, as it is always provided, but it's good to include for clarity
title: { $type: 'string', default: 'Unnamed article' },
text: 'string',
readCount: 'integer',
archived: { $type: 'boolean', default: false },
rating: 'float',
createdAt: 'date',
updatedAt: 'date'
},
access: 'all'
}
Note that if you provide the id field, it should be set to type 'string' orcreatedAt
Integreat will throw. The same happens if you set or updatedAt to'date'
anything else than the type . If you don't include these fields,id
Integreat will include the for you, but not createdAt or updatedAt.
When data is cast to a schema, the data will be in the following format:
``
{
id:
$type:
createdAt:
updatedAt:
...
}
- id: The id is mandatory and created by Integreat when it is not included innull
the schema. If you don't map anything to the id prop, it will be set to
, unless the schema is set up with generateId: true, in which case a$type
universally unique id will be generated for you.
- : Set to the id of the schema by Integreat. This is a signal that thecreatedAt
data has been cast.
- : This is not mandatory, but has special meaning. When a schemacreatedAt
has a field, but the date is not set in the data, it will be setupdatedAt
to the same as (if provided) or to the current date/time.updatedAt
- : Just as createdAt, this is not mandatory. When a schema hasupdatedAt
an field, and the date is not set in the data, it will be set tocreatedAt
the same as (if provided) or the current date/time.
- : Then follows the values of all the fields specified in the schema.castWithoutDefaults
Any value not provided in the data will be set to their default value, unless
is set to true inundefined
the endpoint definition. When casting a value results in
, it will not be included on the returned object. Fields that hasid
the of other schemas as their type, will be objects. If only the id is{ id:
provided in the data, the format will$ref
be used, with being the id of the field type schema. When more data is$type
provided, Integreat will cast it to the target schema and provide the entire
data object, or array of objects, with the relevant .
Set the access property on a schema to enforce permission checking. This
applies to any service that provides data in this schema.
The simplest access rule is auth, which means that anyone can do anythingident
with the data of this schema, as long as they are authenticated. Being
authenticated, in this context, means that the dispatched action has an in the meta object. See the section on idents for more on
this.
Example of a schema with an access rule:
`javascript`
{
id: 'article',
shape: {
// ...
},
access: 'auth'
}
To signal that the schema really has no need for authorization, use all.auth
This is not the same as not setting the prop, as all will overrideall
Integreat's principle of not letting authorized data out of Integreat without
an access rule. allows anybody to access the data, even the
unauthenticated.
On the other end of the spectrum, none will allow no one to access data cast
to this schema, no matter who they are.
For more fine-grained rules, set access to an access definition object withmeta
rules telling Integreat which rights to require when performing different
actions with a given schema. These rules apply to the idents set on
the action object.
The following access rule props are available:
- allow: Set to all, auth, or none, to give access to everybody, onlyrole
the authenticated, or no one at all. This is what we describe in short form
above, where we provided this string instead of a access rule object.
- : Authorize only idents with this role. May also be an array.ident
- : Authorize only idents with this precise id. May also be an array.roleFromField
- : Same as role, except the role is gotten from a field inidentFromField
the schema. When authorizing data cast to this schema, the value of the role
field needs to be identical to (one of) the role(s) of the ident.
- - The same as roleFromField, but for an ident id.
In addition, you may override the general access rules of a schema with
specific rules for a type of action, by setting an action object with accessGET
rules for action types. Here's an example of an access definition for allowing
all authorized idents to data in a certain shema, requiring the roleadmin for SETs, and disallowing all other actions with the general ruleallow: 'none':
`javascript`
{
id: 'article',
shape: {
// ...
},
access: {
allow: 'none',
actions: {
GET: { allow: 'auth' },
SET: { role: 'admin' }
}
}
}
Note that these action specific rules only applies to actions being sent to a
service. Some actions will never reach a service, but will instead trigger
other actions, and access will be granted or rejected only for the actions
that are about to be sent to a service. E.g. when you dispatch a SYNCGET
action, it starts off by dispatching one or more actions. The SYNCGET
action is not subjected to any access rules, but the actions are, and soSYNC
the will fail if one of the GET is rejected.
Another example, intended for authorizing only the ident matching a user:
`javascript`
{
id: 'user',
shape: {
// ...
},
access: { identFromField: 'id' }
}
Here, only actions where the ident id is the same as the id of the user data,
will be allowed. This means that authenticated users (idents) may only
only access their own user data.
Actions are serializable objects that are dispatched to Integreat. It is a
important that they are serializable, as this allows them to, for instance, be
put in a database persisted queue and be picked up of another Intergreat
instance in another process. Note that Date objects are considered
serializable, as they are converted to ISO date strings when needed.
An action looks like this:
`javascript`
{
type:
payload:
meta:
}
- type: This is the id of one of the action handlersGET
that comes with Integreat, e.g. . When you dispatch an action, it ispayload
handed off to this handler (after some inital preperation). You may write
your own action handlers as well.
- : Holds parameters and data for this action. Theremeta
are some reserved payload properties, and the rest
will be made available to you in the mutation pipeline.
- : Holds information about the action that does not belong in the
payload, like the ident of the user dispatching, action id, etc. There are
some reserved meta properties, but you may add your own
here too.
When an action is dispatched, it returns a response object
with status, data, error message, etc.
Note that in a mutation pipeline, action handler, or middleware, the
response object is provided as a fourth property on the action. You will most
likely meet this at least when setting up mutations.
Integreat will keep track of how many actions have been dispatched and are
currently being process. The Instance object (great indispatchedCount
the example at the beginning of this README) has a property that gives you the number dispatched actions waitingdone
to be completed. Every time all dispatched are completed, a event will
be emitted.
The payload is, together with the action type, a description to Integreat and
the service of what to do. A design principle of Integreat has been to have as
little specifics in these payload, so actions may be discpatched to service
without knowing how the service works. This is not always possible, at least
not yet, but it's a good principle to follow, also when you configure services
and plan what props need to be sent in the action payload.
You may set any properties on the payload, and they will be be available to you
in the service endpoint match and in the service mutations. Some properties
have special meanings, though, and you should avoid using them for anything
else:
- type: The type of the data the action sends and/or receives. This refers toid
the of a schema, and you will usually want to set this. Data provideddata
in the payload and response data will be cast to this schema. IfallowRawRequest
you're dealing with several types in one action, you may set an array of
types, but will have to cast the data in the mutation yourself. Integreat
will validate that the data you send and receive is indeed of that type, and
will give you an auth error if not. (See
and allowRawResponse on endpoints for anid
exception.)
- : You provide an id when you want to address a specific data item,{ type: 'GET', payload: { type: 'article', id: '12345' } }
usually when you want to fetch one data item with an action like
. You may alsodata
supply an array of ids to fetch several data items by id. When setting data,
the id will instead be specified in the when appropriate.data
- : The data to send to a service. This may be any data that makes senseservice
to the service, but will usually be a typed data object or an
array of typed data objects, where the adjustments for the service happens in
service mutations.
- : The id of the service to send this action to. If not specified,type
Integreat will try and find the right service from the .targetService
- : An alias of service.sourceService
- : When data comes from a different service and has not beensourceService
mutated and cast yet, the property will tell Integreat to runlisten()
the data through the source service configuration before passing the action
on to an action handler. An example may be data coming in through an API,
where the API is configured as a service in Integreat. Note that this
property is usually set by transporters in their methods, but youendpoint
may also set it directly on the action when it makes sense.
- : Set this to the id of a service endpoint when you want to
override the endpoint match rules of Integreat. This should only be used when
it is really necessary. Normally, you should instead design the match
properties to match the correct actions.
For services that support pagination, i.e. fetching data in several rounds, one
page at a time, the following properties may be supported:
- pageSize: The number of data items to fetch in one request to the service.pageOffset
By specifying a page size, you signal that you would like to use pagination,
and without it all other pagination properties will be disregarded. You will
get the number of data items you specify (or less, if there are not that many
items), and may then go on to dispatch an action for the next page. See
pagination for more
- : The number of data items to "skip" before returning the numberpageSize
of items specified in . If you ask for 500 items, the first actionpageOffset: 0
should have (or not specified), the next actionpageOffset: 500
, then pageOffset: 1000, and so on.page
- : The index of the page to fetch. Unlike most other indexes, this1
starts with being the first page. The effect is the same aspageOffset
, it's just a different way of specifying it. page: 1 is thepageOffset: 0
same as , and page: 2 is the same as pageOffset: 500,pageSize: 500
given a . Integreat will actually calculate both beforepageAfter
sending it to the transporter, as different types of services support
different types of pagination.
- : As an alternative to specifying the number of items to skip, youpageAfter
may ask for the items after the item with the id you provide as .'12345'
If the last item of the first page is , you may setpageAfter: '12345'
to get the next page.pageBefore
- : This works the same as pageAfter, except it is intended forpageId
when your going backward and fetching a number items _before_ the id you
provide.
- : Some services and/or transporters will return an id for the nextpageId
page, as an alternative to the other properties mentioned above. You then
apply this id as when dispatching the action for the next page. Note
that this id may hold internal logic from the transporter, but you should
never rely on this logic and simply use it as an id.
> [!IMPORTANT]
> Pagination has to be supported by the service and your
> service configuration, and sometimes also the transporter. Integreat prepares
> and passes on these pagination properties, but if the service disregards
> them, there is little Integreat can do – except limiting the number of items
> returned. It's up to you to figure out how to configure pagination for a
> service, but youshould use these pagination properties to support it, to make
> this predictable. It also lets you use actions such as GET_ALL, that
> support pagination.
Finally, there are some properties that has no special meaning to Integreat
itself, but that may be set on incoming actions from transporters. These should
ideally be used in the same way or avoided:
- contentType: A keyword for the type of content in the data property. E.g.application/json
or text/plain.headers
- : An object of header information, given as key/value pairs. Thehostname
value may be a string or an array of strings. This may be HTTP headers or any
other type of header information that makes sense to a service.
- : The host name that incoming request was sent to. For HTTP, thismethod
will be the domain name the request was sent to.
- : The method of the incoming request. The HTTP transporter will setGET
this to , POST, PUT, etc. from the incoming request.path
- : The path from the incoming request. For the HTTP transporter, this'/v1.0/articles/12345'
will be the part of the url after the domain name, like
.port
- : The port number of the incoming request.queryParams
- : An object of query params from the incoming request, usually
key/value pairs where the value is a string or an array of strings. For HTTP,
this will be the part after the question mark.
The action meta object is for information about an action that does not
directly define the action itself. The difference may be subtle in some cases,
but the general rule is a piece of information affects how the action is run,
it should be in the payload. E.g. the type of items to fetch is in the
payload, while the time the action was dispatched would go in the meta.
This rule does not always hold, e.g. for information on the user dispatching
the action in ident on the meta object. Different idents may result in
different data being returned from the service, but still the action to
perform is the same, so it makes sense to have the ident in the meta object.
You may set your own meta properties, but in most cases you'll probably rather
set payload properties.
Current meta properties reserved by Integreat:
- ident: The ident to authorize the action with. May hold an id, roles,tokens
, and a few other options. Seeid
the section on idents.
- : The id of the action itself. You may set this yourself or let Integreatcid
generate a universally unique id for you. Useful for logging and may be used
by queues.
- : Correlation id. When dispatching an action without meta.cid,meta.id
Integreat will set it to the same as the . All actions that are thenSYNC
dispatched as a consequence of that action (e.g. a or GET_META),cid
will have the same . The cid may then be used to group actionscid
belonging together, e.g. when displaying logs. The dispatching code set the
on an action, e.g. to correlate an action and the actions itgid
dispatches, with other operations outside Integreat.
- : Group id. This has some of the same purpose as cid, as it may begid
used to group actions that belong together, but is not always set, andcid
will be used for smaller groups of actions than . Right now, RUN,SYNC
, and GET_ALL will use the id of the original action as gid fordispatchedAt
all actions they dispatched.
- : Timestamp for when the action was dispatched (set byqueue
Integreat).
- : Signals to Integreat that an action may be queued. Set to truequeuedAt
when you want the action to be queued, but executed as soon as possible. Set
to a UNIX timestamp (number) to schedule for a later time. If no queue is
set up, the action will be dispatched right away. More on this under
the section on queues.
- : Timestamp for when the action was pushed to the queue (set byoptions
Integreat).
- : Used for passing the processed service endpoint options object tooptions
a transporter. The object is available through mutations, so thatauthorized
you may modify it futher before it goes to the transporter. Note that only
the transporter options are provided here, not the adapter options.
- : An internal flag signaling that the action has been authorized.
Will be removed from any dispatched actions.
When you dispatch an action, you will get a response object back in this
format:
`javascript
{
status:
data: ,
error:
warning:
origin: `
access:
paging:
params:
headers:
responses:
}
- status: The status of the action. Will be ok when everything went well,data
see list of status codes below for more.
- : Any data returned from the service, after being modified by thetype
mutation pipelines from your service and endpoint configuration. It will be
cast to typed data through the schema specified by the payload
, if it is set to a single type and the endpoint allowRawResponse istrue
not set to .error
- : All error statuses (i.e. not ok or queued) will return an errorwarning
message, some may include error messages from the service.
- : When the action was successful, but there still was something youorigin
should know, the warning message is where you'll get noticed. An example is
when you get an array of data items, but some of them was removed due to the
access of the ident on the action.
- : When the response is an error (status is not 'ok' or 'queue'),access
this property will hold a code for where the error originated. The goal is to
set it as close to the actual origin as possible. See
list of origin codes below.
- : An object holding the ident that was actually being used. Thismeta.ident
may be different than the on the action, as the ident may alsopaging
be mutated or completed with roles etc. along the way.
- : For services and transporters that supportnext
pagination, this object will hold information about how to get
the next or previous page, in a or prev object. These objects aretype
essentially the payloads you need to dispatch (with the same action paging
and meta), to get the next or previous page. If there is no next or previous
page, the corresponding prop will not be set on the object. Whenpaging
pagination is not relevant or used, the object may be missingparams
completely.
- : Integreat never sets this, but you may set it in your mutations todata
provide parameters from a service that does not belong in the .headers
- : Integreat never sets this, but you may set it in your mutations toresponses
provide header key/value pairs from a service. Typically used when this is a
response to an incoming request that support headers, like HTTP do.
- : In some cases, an action will run several sub-actions, likeSYNC
or RUN. The action handlers _may_ then provide an array of all the
sub-response objects here.
> [!NOTE]
> Editor's note: Is it correct that queues return the id in the data?
When the status is queued, the id of the queued action may found inresponse.data.id. This is the id assigned by the queue, and not necessarilyaction.meta.id
the same as .
The status property on the action response will be one of the following
status codes:
- ok: Everything is well, data is returned as expectedqueued
- : The action has been queued. This is regarded as a success statusnoaction
- : The action did nothing, e.g. when a SYNC` a