Releases: 42BV/mad-spring-connect
v4.3.0
- feat: the id field of a makeResource has been made generic. 62f670e
This allows the id field to become a string for example:
class Pokemon extends makeResource<Pokemon, string>('/api/pokemon') {
public id?: string;
public name!: string;
public types!: string[];
}
const pokemon = new Pokemon();
// This should now work because the type of ID is now a string.
pokemon.id = 'a-unique-uu-id-for-example';
pokemon.name = 'bulbasaur';
pokemon.types = ['poison', 'grass'];
v4.2.1
v4.2.0
v4.1.0
feat: It is now possible to have a custom mapper.
Resource now accepts an configuration object, which can contain a
custom mapper. With the custom mapper it is possible to dynamically
add properties and to convert them.
For example:
class Pokeball extends makeResource<Pokeball>({
baseUrl: '/api/pokeball',
mapper: pokeballMapper,
}) {
public id?: number;
/*
In the actual JSON response pokemon is simply an object.
But our custom mapper makes sure it will also get mapped.
*/
public pokemon!: Pokemon;
/*
Does not really exist on the back-end but is filled by the
custom mapper.
*/
public retrievedAt!: Date;
}
function pokeballMapper(json: any, Class: { new (): Pokeball }): Pokeball {
const pokeball = makeInstance(Class, json);
/* Add a completely new field */
pokeball.retrievedAt = new Date();
/* Make the fetched pokemon an actual instance of Pokemon */
pokeball.pokemon = makeInstance(Pokemon, pokeball.pokemon);
return pokeball;
}
4.0.0
makeResource
The makeResource
should now be used like this:
class Pokemon extends makeResource<Pokemon>('api/pokemon')
Instead of the old way:
class Pokemon extends makeResource('api/pokemon')<Pokemon>
By putting the generic directly after makeResource
TypeScript actually
recognizes the return types correcty. Before calling Pokemon.one
would
give back a Promise<unknown>
instead of Promise<Pokemon>
.
This way we no longer need to tell TypeScript the type when
using it:
const pokemon: Pokemon = await Pokemon.one(1);
And we can instead we can simply do:
// The type of pokemon is now correctly inferred as Pokemon
const pokemon = await Pokemon.one(1);
findeOne
The findOne
method now returns a Promise
of T
or void
. This
way TypeScript recognizes the return type better, it did not understand
null
correctly, and acted as if it was a T
. Now TypeScript will make
you check if you actually have a type T or empty. This was actually a
bug discovered by the better type inference.
Export of Resource
No longer exporting a Resource
class definition. It got out of
sync really easily with the actual return value, so it has been
removed.
3.3.0
v3.2.0
The post, patch and put payload parameter can now be a FormData. Useful for totally controlling the body for example when uploading.
Updated to TypeScript 3.5.3 had to make QueryParams the correct type as needed by query-string's stringify method.
Exposed Method, MiddlewareDetailInfo, QueryParams in the index.
v3.1.2
v3.1.0
Added findOne method to Resource, it allows you to search for a single resource.
See: https://42bv.github.io/mad-spring-connect/#searching-for-one-pokemon