@ngx-pwa/local-storage
Advanced tools
Changelog
11.1.0 (2021-01-21)
No library change, just rebuilt with Angular 11.1 (still compatible with Angular 11.0).
Changelog
11.0.2 (2020-12-23)
No library change, just a fix in schematics to avoid ng add
breaking with Angular 11.1.
Changelog
11.0.1 (2020-11-13)
No change, just a release to update link to new main
branch.
Changelog
11.0.0 (2020-11-12)
A full migration guide to version 11 is available.
If you did not update to version 9 yet, be sure to follow it, as otherwise you could lose all previously stored data.
Supports and requires Angular 11.
TypeScript typings for .get()
and .watch()
has been modified to better match the library behavior.
For now, wrong usages are just marked as deprecated, so there is no breaking change and it will just be reported by linters. But they may be removed in future releases.
Be sure to read the validation guide for all the why and how of validation.
this.storage.get<User>('user');
was allowed but the result was still unknown
.
This is a very common misconception of client-side storage: you can store and get anything in storage, so many people think that casting as above is enough to get the right result type. It is not.
Why? Because you are getting data from client-side storage: so it may have been modified (just go to your browser developer tools and hack what you want).
A cast is just an information for compilation:
it basically says to TypeScript: "believe me, it will be a User
".
But that's not true: you are not sure you will get a User
.
This is why this library provides a runtime validation system, via a JSON schema as the second parameter:
this.storage.get<User>('user', {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name'],
});
this.storage.get('user', {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name'],
});
was allowed but the result was still unknown
.
This is because, for now, the library is able to infer the return type based on the JSON schema
for primitives (string
, number
, integer
, boolean
and array
of these),
but not for more complex structures like objects.
So in this case, both the JSON schema and the cast are required:
this.storage.get<User>('user', {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name'],
});
Be aware you are responsible the casted type (User
) describes the same structure as the JSON schema.
For the same reason, the library cannot check that.
Auto-inferring the type from all JSON schemas is in progress in #463.
this.storage.get<number>('name', { type: 'string' });
was allowed but is of course an error. Now the match between the cast and simple JSON schemas (string
, number
, integer
, boolean
and array
of these) is checked.
Note that in this scenario, the cast is not needed at all, it will be automatically inferred by the lib, so just do:
this.storage.get('name', { type: 'string' });
as const
assertionGiven how JSON schema works, sometimes it is better to set them as const
:
this.storage.get('name', { type: 'string' } as const);
But before v11, it was not possible when using some JSON schema properties
(enum
, items
, required
). This is now fixed.
Changelog
10.1.0 (2020-09-03)
No code change, just rebuilt with Angular 10.1.