This guide allows you to know how to migrate from @lagoshny/ngx-hal-client
to @lagoshny/ngx-hateoas-client
.
Why you may need does it see in the motivation section.
- Motivation
- Migration steps
- Library changes
The main reason to create a new hateoas library is @lagoshny/ngx-hal-client
was a fork from angular4-hal
library and now angular4-hal
is not supported.
To adds new features or refactoring fork version @lagoshny/ngx-hal-client
is not the right way because it inherits all code from parent angular4-hal
and the main problem is angular4-hal
has not a good documentation and has some not documented features.
Lack of tests and documentation does not allow refactoring, implementing new functions and developing the old project.
New library rewritten taking only the best principles from @lagoshny/ngx-hal-client
fork.
Some new features:
- A simplified configuration: now you inject configuration service and pass configuration as JS object instead of create a standalone configuration class with configuration interface implementation. See more in configuration.
- Added ability to debug library with logging feature.
- Changed support for subtypes now is not need to pass subtypes object to method param because each Resource class has own resource type.
- Added HateoasResourceService is standalone generic resource service, only need to set generic param to resource type and use it without create a resource service class.
- Added support for resource projection
- Added more than 400 tests.
- Added documentation describing all library features.
- And much more.
It is strongly recommend migrate from @lagoshny/ngx-hal-client
to @lagoshny/ngx-hateoas-client
to be able to use new features.
Old
@lagoshny/ngx-hal-client
will not develop, will only bug fixes.
-
The first migration step is delete
@lagoshny/ngx-hal-client
frompackage.json
file and install@lagoshny/ngx-hateoas-client
by the commandnpm i @lagoshny/ngx-hateoas-client --save
-
After that delete old
@lagoshny/ngx-hal-client
configuration and add a new@lagoshny/ngx-hateoas-client
configuration using the same configuration params. -
Now need change class imports from
@lagoshny/ngx-hal-client
to@lagoshny/ngx-hateoas-client
.To do it you can use
replaceAll
command with a code editor. -
Do all
RestService
changes described below.Find info about methods param changes in this section.
-
Do all
Resource
changes described below. -
Do all
EmbeddedResource
changes described below.Find info about methods param changes in this section.
-
Do all
ResourcePage
changes described belowFind info about methods param changes in this section.
Congratulations! It is all that you need to do to migrate to @lagoshny/ngx-hateoas-client
.
You can found an example of the migration process by this commit from one of my applications.
If you have some questions or found a bug you can create new issue here.
This section describes the main library changes compare with @lagoshny/ngx-hal-client
library.
The next changes were in the lib configuration:
The old configuration ExternalConfigService
class was delete.
Added new NgxHateoasClientConfigurationService
class has the method configure
to pass app configuration.
How configure library with NgxHateoasClientConfigurationService
see here.
Class RestService
renamed to HateoasResourceOperation
.
The next common changes was in RestService
(now is HateoasResourceOperation
class):
- Deleted constructor params:
injector
andresourceName
, now onlytype
param. - Deleted page methods:
totalElement
,totalPages
,hasFirst
,hasNext
,hasPrev
,hasLast
,next
,prev
,first
,last
,page
. See PagedResourceCollection to use these methods. - Deleted
getByRelation
andgetByRelationArray
use getRelation and getRelatedCollection from Resource class instead. - Deleted
customQueryPost
use customQuery instead. - Deleted
count
use customQuery instead. - Deleted
getBySelfLink
use getResource instead
Renamed get
to getResource
and changed method signature:
Was:
get(id: any, params?: HalParam[]): Observable<T>;
Now:
getResource(id: number | string, options?: GetOption): Observable<T>;
- Changed
id
param type fromany
tonumber|string
- Renamed
params
tooptions
and changed type fromHalParam[]
to GetOption.
See more about
GetResource
method here.
Renamed getAll
to getCollection
and changed method signature:
Was:
getAll(options?: HalOptions, subType?: SubTypeBuilder): Observable<T[]>;
Now:
getCollection(options?: GetOption): Observable<ResourceCollection<T>>;
- Changed
options
type fromHalOptions
to GetOption, see here how to changeHalOptions
toGetOption
. - Deleted
subType
param, subtypes support, see here.
See more about
GetCollection
method here.
Renamed getAllPage
to getPage
and changed method signature:
Was:
getAllPage(options?: HalOptions, subType?: SubTypeBuilder): Observable<ResourcePage<T>>;
Now:
getPage(options?: PagedGetOption): Observable<PagedResourceCollection<T>>;
- Changed
options
type fromHalOptions
to PagedGetOption, see here how to changeHalOptions
toPagedGetOption
. - Deleted
subType
param, subtypes support, see here. - Changed return value from
ResourcePage
to PagedCollectionResource see classes changes.
See more about
GetPage
method here.
Renamed search
to searchCollection
and changed method signature:
Was:
search(query: string, options?: HalOptions, subType?: SubTypeBuilder): Observable<T[]>;
Now:
searchCollection(query: string, options?: GetOption): Observable<ResourceCollection<T>>;
- Changed
options
type fromHalOptions
to GetOption, see here how to changeHalOptions
toGetOption
. - Deleted
subType
param, subtypes support, see here. - Changed return type from
Array<Resource>
to ResourceCollection.
See more about
SearchCollection
method here.
searchPage
changed method signature:
Was:
searchPage(query: string, options?: HalOptions, subType?: SubTypeBuilder): Observable<ResourcePage<T>>;
Now:
searchPage(query: string, options?: PagedGetOption): Observable<PagedResourceCollection<T>>;
- Changed
options
type fromHalOptions
to PagedGetOption, see here how to changeHalOptions
toPagedGetOption
. - Deleted
subType
param, subtypes support, see here. - Changed return type from
ResourcePage
to PagedCollectionResource.
See more about
SearchPage
method here.
Renamed searchSingle
to searchResource
and changed method signature:
Was:
searchSingle(query: string, options?: HalOptions): Observable<T>;
Now:
searchResource(query: string, options?: GetOption): Observable<T>;
See more about
SearchResource
method here.
customQuery
changed method signature:
Was:
customQuery(query: string, options?: HalOptions, subType?: SubTypeBuilder): Observable<T[]>;
Now:
customQuery<R>(method: HttpMethod, query: string, requestBody?: RequestBody<any>, options?: PagedGetOption): Observable<R>;
- Added generic param
<R>
that define return type instead of oldArray<Resource>
. - Added
method
param that defined the HTTP query method. - Added requestBody param allows pass request body (used with PATCH, PUT, POST methods).
- Changed
options
type fromHalOptions
to PagedGetOption, see here how to changeHalOptions
toPagedGetOption
. - Deleted
subType
param, subtypes support, see here.
See more about
CustomQuery
method here.
Renamed create
to createResource
and changed method signature:
Was:
create(entity: T);
Now:
createResource(requestBody: RequestBody<T>): Observable<T>;
- Renamed
entity
param torequestBody
and changed type fromT
to RequestBody.
See more about
CreateResource
method here.
Renamed update
to updateResource
and changed method signature:
Was:
update(entity: T);
Now:
updateResource(entity: T, requestBody?: RequestBody<any>): Observable<T | any>;
- Added RequestBody param to pass part of
entity
values to change.
See more about
UpdateResource
method here.
Renamed patch
to patchResource
and changed method signature:
Was:
patch(entity: T, options?: Array<ResourceOptions> | Include);
Now:
patchResource(entity: T, requestBody?: RequestBody<any>): Observable<T | any>;
- Changed
options
type to requestBody.
See more about
PatchResource
method here.
Renamed delete
to deleteResource
and changed method signature:
Was:
delete(entity: T): Observable<object>;
Now:
deleteResource(entity: T, options?: RequestOption): Observable<HttpResponse<any> | any>;
- Added
options
param with type RequestOption that allowed to define request return typeobserve
orresponse
and pass request params.
See more about
DeleteResource
method here.
Deleted handleError
method, if you need, define the same method in your project:
handleError
is simple wrapper:
protected handleError(error: any): Observable<never> {
return observableThrowError(error);
}
This section contains Resource
class changes.
Now each Resource
class should have a @HateasoResource
decorator that accepts resourceName
param. You cas see more about this here
Was:
export class Product extends Resource {
...
}
Now:
@HateoasResource('products')
export class Product extends Resource {
...
}
getRelation
method signature changes:
Was:
getRelation<T extends BaseResource>(type: {new (): T;}, relation: string, builder?: SubTypeBuilder, expireMs?: number, isCacheActive?: boolean): Observable<T>;
Now:
getRelation<T extends BaseResource>(relationName: string, options?: GetOption): Observable<T>;
- Renamed
relation
param torelationName
. - Deleted
type
,builder
(more about subtypes here),expireMs
,isCacheActive
(more about cache support here). - Added
options
GetOption param.
See more about
GetRelation
method here.
Removed getProjection
method, use getRelation with options: {params: {projection: 'projectionName'}}
.
See more about
GetRelation
method here.
Renamed getRelationArray
to getRelatedCollection
and changed method signature:
Was:
getRelationArray<T extends BaseResource>(type: { new(): T }, relation: string, options?: HalOptions, embedded?: string, builder?: SubTypeBuilder, expireMs: number = 0, isCacheActive: boolean = true): Observable<T[]>;
Now:
getRelatedCollection<T extends ResourceCollection<BaseResource>>(relationName: string, options?: GetOption): Observable<T>;
- Renamed
relation
param torelationName
. - Deleted
type
,embedded
(is not supported anymore),builder
(more about subtypes here),expireMs
,isCacheActive
(more about cache support here). - Changed
options
type fromHalOptions
to GetOption, see here how to changeHalOptions
toGetOption
. - Changed return type from
Array<Resource>
to ResourceCollection.
See more about
GetRelatedCollection
method here.
Removed getProjectionArray
method, use getRelatedCollection with options: {params: {projection: 'projectionName'}}
.
See more about
GetRelatedCollection
method here.
addRelation
changed method name and signature:
Was:
addRelation<T extends BaseResource>(relation: string, resource: T): Observable<any>;
Now:
addCollectionRelation<T extends Resource>(relationName: string, entities: Array<T>): Observable<HttpResponse<any>>;
- Renamed
relation
param torelationName
. - Renamed
resource
param toentities
and changed param type to an array.
See more about
addCollectionRelation
method here.
Renamed substituteRelation
to bindRelation
and some method changes:
- Renamed
relation
param torelationName
. - Renamed
resource
param toentities
and changed param type to an array.
See more about
BindRelation
method here.
deleteRelation
method changes:
- Renamed param
relation
torelationName
- Renamed param
resource
toentitiy
See more about
DeleteRelation
method here.
Renamed clearCollectionRelation
to unbindCollectionRelation
and some method changes:
- Renamed
relation
param torelationName
.
See more about
unbindCollectionRelation
method here.
postRelation
changed method signature:
Was:
postRelation(relation: string, body: any, options?: LinkOptions): Observable<any>;
Now:
postRelation(relationName: string, requestBody: RequestBody<any>, options?: RequestOption): Observable<any>;
- Renamed
relation
torelationName
. - Renamed
body
param torequestBody
and change type fromany
to RequestBody. - Change
options
param type fromLinkOptions
to RequestOption.
See more about
PostRelation
method here.
patchRelation
changed method signature:
Was:
patchRelation(relation: string, body: any, options?: LinkOptions): Observable<any>;
Now:
patchRelation(relationName: string, requestBody: RequestBody<any>, options?: RequestOption): Observable<any>;
- Renamed
relation
torelationName
. - Renamed
body
param torequestBody
and change type fromany
to RequestBody. - Change
options
param type fromLinkOptions
to RequestOption.
See more about
PatchRelation
method here.
Now each EmbeddedResource
class should have a @HateasoEmbeddedResource
decorator that accepts relationNames
param. You cas see more about this here
Was:
export class Client extends Resource {
...
public clientAddress: Address;
...
}
export class Address extends EmbeddedResource {
...
}
Now:
@HateoasResource('clients')
export class Client extends Resource {
...
public clientAddress: Address;
...
}
@HateoasEmbeddedResource(['clientAddress'])
export class Address extends EmbeddedResource {
...
}
ResourcePage
class renamed to PagedCollectionResource and has the next changes:
sortElements
changed method signature:
Was:
sortElements(...sort: Sort[]): Observable<ResourcePage<T>>;
Now:
sortElements(sortParam: Sort, options?: { useCache: true }): Observable<PagedResourceCollection<T>>;
- Changed
sort
param type, new type is object, more see here.
See more about
SortElements
method here.
CacheHelper
class does not exist anymore, use NgxHateoasClientConfigurationService
to configure cache settings.
HalParam
class replaced to GetOption or PagedGetOption classes.
Everywhere was used HalParam
now need to use GetOption
or GetPagedOption
depends on return value when return value is PagedResourceCollection
then will be PagedGetOption
, GetOption
otherwise.
Example of migration from HalParam
to GetOption
or PagedGetOption
:
HalParam
representation:
params: [
{
key: 'projection',
value: 'testProjection'
},
{
key: 'param1',
value: 'value1'
},
{
key: 'param2',
value: 'value2'
}
]
After converting to GetOption
or PagedGetOption
:
params: {
projection: 'testProjection',
param1: 'value1',
param2: 'value2'
}
HalOptions
class replaced to GetOption or PagedGetOption classes.
Everywhere was used HalOptions
now need to use GetOption
or GetPagedOption
depends on return value when return value is PagedResourceCollection
then will be PagedGetOption
, GetOption
otherwise.
Example of migration from HalOptions
to GetOption
or PagedGetOption
:
HalOptions
representation:
{
size: 20,
params: [
{
key: 'projection',
value: 'testProjection'
},
{
key: 'param1',
value: 'value1'
},
{
key: 'param2',
value: 'value2'
}
],
sort: [
{path: 'test', order: 'DESC'}
],
notPaged: false
}
After converting to GetOption
:
{
params: {
projection: 'testProjection',
param1: 'value1',
param2: 'value2
},
sort: {
test: 'DESC'
}
}
Note!
GetOption
does not contain asize
param because it is a page param.
After converting to PagedGetOption
:
{
params: {
projection: 'testProjection',
param1: 'value1',
param2: 'value2
},
sort: {
test: 'DESC'
},
pageParams: {
size: 20
}
}
LinkOptions
replaced to RequestOption
params.
Example of migration from LinkOptions
to RequestOption
:
LinkOptions
representation:
{
strictParams?: boolean,
params: {
param1: 'value1',
param2: 'value2
}
}
After converting to PagedGetOption
:
{
params: {
param1: 'value1',
param2: 'value2
}
observe?: 'body' | 'response';
}
strictParams
was delete andobserve
was add that allows change response type fromraw body
toAngular HttpResponse
.
Sort param was changed from array to object notation.
Was:
sort: [
{path: 'test', order: 'DESC'}
]
Now:
sort: {
test: 'DESC'
}
SubTypeBuilder
param is not exist anymore, use Resource.isResourceOf method to know which resource type you got.
See more about support subtypes here.
expireMs
, isCacheActive
params are not exist anymore.
How to manage the cache see here.