A parser for REST API responses following a provided schema.
Creates a new instance of the SchemaParser
.
constructor( { schema, dataKey, metaKey } ): SchemaParser
The SchemaParser
constructor
receives an hash parameter with the following properties:
Name | Type | Description | Required | Default |
---|---|---|---|---|
schema | SchemaEntity | A SchemaEntity instance with the schema for entity that will be parsed. |
yes | - |
dataKey | string |
The key for the response data. | yes | - |
metaKey | string |
The key for the response metadata. | yes | - |
Returns a new instance of the SchemaParser
.
const stateSchema = new SchemaEntity( {
type: 'state',
mapper: { id: 'id', name: 'name' }
} );
const citySchema = new SchemaEntity( {
type: 'city',
mapper: ( { id, name, state_id } ) => ( { id, name, stateId: state_id } ),
relations: { state: stateSchema }
} );
const schema = new SchemaEntity( {
type: 'user',
mapper: {
id: 'id',
first_name: 'firstName',
last_name: 'lastName',
age: 'age',
city_id: 'cityId',
favorite_states_ids: 'favoriteStatesIds'
},
relations: {
city: citySchema,
favorite_states: stateSchema
}
} );
const parser = new SchemaParser( { schema, dataKey: 'response', metaKey: 'meta' } );
Parses the passed api response.
parse( responseToParse ): ParsedResponse
Name | Type | Description | Required | Default |
---|---|---|---|---|
responseToParse | JSONData | The response to parse. | yes | - |
Returns the parsed response.
const response = {
response: {
id: '1',
first_name: 'Homer',
last_name: 'Simpson',
age: 36,
city: {
id: 3,
name: 'Springfield',
state: {
id: 8,
name: 'Illinois'
}
},
favorite_states: [
{
id: 7,
name: 'Indiana'
},
{
id: 8,
name: 'Illinois'
}
]
}
};
const parser = new SchemaParser( { schema, dataKey: 'response', metaKey: 'meta' } );
const parsedResponse = parser.parse( response );
// {
// data: {
// type: 'user',
// attributes: {
// id: 1,
// firstName: 'Homer',
// lastName: 'Simpson',
// age: 36,
// cityId: 3
// }
// },
// included: [
// {
// type: 'city',
// attributes: {
// id: 3,
// name: 'Springfield',
// stateId: 8
// }
// },
// {
// type: 'state',
// attributes: {
// id: 8,
// name: 'Illinois'
// }
// },
// {
// type: 'state',
// attributes: {
// id: 7,
// name: 'Indiana'
// }
// }
// ]
// }
Defines the data schema for a response entity.
Creates a new instance of the SchemaEntity
.
constructor( { type, mapper, relations } ): SchemaEntity
The SchemaParser
constructor
receives an hash parameter with the following properties:
Name | Type | Description | Required | Default |
---|---|---|---|---|
type | EntityType | The type for this entity. This type will be used later by the creator to know in which store to create the entity. | yes | - |
mapper | AttributesMapper | An object defining how attributes for this entity are going to be mapped. | yes | - |
relations | SchemaRelations | An object defining the scheme entity for the relations nested in the response. | no | {} |
Returns a new instance of the SchemaEntity.
const stateSchema = new SchemaEntity( {
type: 'state',
mapper: { id: 'id', name: 'name' }
} );
const citySchema = new SchemaEntity( {
type: 'city',
mapper: ( { id, name, state_id } ) => ( { id, name, stateId: state_id } ),
relations: { state: stateSchema }
} );
const schema = new SchemaEntity( {
type: 'user',
mapper: {
id: 'id',
first_name: 'firstName',
last_name: 'lastName',
age: 'age',
city_id: 'cityId',
favorite_states_ids: 'favoriteStatesIds'
},
relations: {
city: citySchema,
favorite_states: stateSchema
}
} );
Maps the attributes of the entity data using the passed mapper.
map( serialization ): Serialization
Name | Type | Description | Required | Default |
---|---|---|---|---|
serialization | Serialization | The data to map. | yes | - |
Returns the mapped serialization.
const userData = {
id: '1',
first_name: 'Homer',
last_name: 'Simpson',
age: 36,
cityId: 3
};
const mappedData = userSchemaEntity.map( userData );
// {
// id: 1,
// firstName: 'Homer',
// lastName: 'Simpson',
// age: 36,
// cityId: 3
// }