Skip to content

Commit 5c225fe

Browse files
authored
feat: new getItem() signature (#79)
1 parent 0a91aed commit 5c225fe

12 files changed

+512
-151
lines changed

CHANGELOG.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ This lib is fully documented and so you'll find detailed [migration guides](./MI
44

55
## 8.0.0-beta.3 (2019-02-07)
66

7+
**A [full migration guide to version 8](./docs/MIGRATION_TO_V8.md) is available.**
8+
79
### Features
810

11+
- The schema used for validation can (and should) be passed directly as the second argument fo `getItem()`
912
- The returned type of `getItem()` is now inferred for basic types (`string`, `number`, `boolean`)
10-
and arrays of basic types (`string[]`, `number[]`, `boolean[]`).
11-
- Just use the new `JSONSchema` interface, IntelliSense will adjust itself based on the `type` option.
13+
and arrays of basic types (`string[]`, `number[]`, `boolean[]`)
14+
- Just use the new `JSONSchema` interface, IntelliSense will adjust itself based on the `type` option
1215
- `indexedDB` database and object store names default values are exported and can be changed
1316
(see the [interoperability guide](./docs/INTEROPERABILITY.md))
1417
- `indexedDB` storage will now works in web workers too
1518

1619
### Breaking changes
1720

18-
A [full migration guide to version 8](./docs/MIGRATION_TO_V8.md) is available.
19-
2021
- `type` now required for array, object, const and enums validation schemas
2122
- `setItem()` and `setItemSubscribe()` no longer accept `null` or `undefined` when in `--strictNullChecks`
2223
- `JSONSchemaNull` removed
@@ -26,6 +27,7 @@ A [full migration guide to version 8](./docs/MIGRATION_TO_V8.md) is available.
2627
### Non-breaking changes
2728

2829
- `JSONSchemaNumeric` deprecated
30+
- `LSGetItemsOptions` deprecated (not necessary anymore)
2931

3032
### Reduced public API
3133

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,10 @@ If you tried to store `undefined`, you'll get `null` too, as some storages don't
127127

128128
Don't forget it's client-side storage: **always check the data**, as it could have been forged or deleted.
129129

130-
Starting with *version 5*, you can use a [JSON Schema](http://json-schema.org/) to validate the data.
131-
132-
**Starting with *version 7*, validation is now required.**
133-
A [migration guide](./docs/MIGRATION_TO_V7.md) is available.
130+
You can use a [JSON Schema](http://json-schema.org/) to validate the data. **Starting with *version 7*, validation is now required.**
134131

135132
```typescript
136-
this.localStorage.getItem('test', { schema: { type: 'string' } })
133+
this.localStorage.getItem('test', { type: 'string' })
137134
.subscribe((user) => {
138135
// Called if data is valid or null
139136
}, (error) => {

docs/MIGRATION_TO_V7.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ So you ended up with a `string` type while the real data may not be a `string` a
8383

8484
If you were not already validating your data, there are several options.
8585

86-
### Solution 1: JSON schema validation (recommended)
86+
### Solution 1: JSON schema validation with v8 (recommended)
87+
88+
Version 8 of the lib greatly simplifies validation. So if you're not yet on v7,
89+
we strongly recommend you to to [upgrade to v8 directly](./MIGRATION_TO_V8.md),
90+
and to follow the [new validation guide](./VALIDATION.md) instead.
91+
92+
### Solution 2: JSON schema validation with v7 (quite painful)
8793

8894
The simpler and better way to validate your data is to search `getItem` in your project
8995
and **use the JSON schema option proposed by the lib**. For example:
@@ -96,9 +102,9 @@ this.localStorage.getItem<string>('test', { schema: { type: 'string' } })
96102
});
97103
```
98104

99-
**A [full validation guide](./VALIDATION.md) is available with all the options.**
105+
**A [full validation guide](./VALIDATION_BEFORE_V8.md) is available with all the options.**
100106

101-
### Solution 2: custom validation (painful)
107+
### Solution 3: custom validation (very painful)
102108

103109
You can use all the native JavaScript operators and functions to validate. For example:
104110

@@ -138,7 +144,7 @@ this.localStorage.getItem('test').subscribe((unsafeResult) => {
138144
});
139145
```
140146

141-
### Solution 3: defer the upgrade (temporary)
147+
### Solution 4: defer the upgrade (temporary)
142148

143149
**Version 6 of the library is compatible with Angular 7.**
144150
So you can upgrade to Angular 7 now and defer the upgrade of this lib,
@@ -147,7 +153,7 @@ to have some extra time to add validation.
147153
Of course, it should be a temporary solution as this scenario is *not* heavily tested,
148154
and as you'll miss new features and bug fixes.
149155

150-
### Solution 4: no validation (dirty)
156+
### Solution 5: no validation (dirty)
151157

152158
In some special scenarios, like development-only code,
153159
it could be painful to manage validation.
@@ -163,6 +169,6 @@ as this method as been flagged as deprecated.
163169
## More documentation
164170

165171
- [Full changelog for v7](../CHANGELOG.md)
166-
- [Full validation guide](./VALIDATION.md)
172+
- [Full validation guide](./VALIDATION_BEFORE_V8.md)
167173
- [Other migration guides](../MIGRATION.md)
168174
- [Main documentation](../README.md)

docs/MIGRATION_TO_V8.md

+43-19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ npm install @ngx-pwa/local-storage@next
2424
```
2525

2626
2. Start your project: problems will be seen at compilation.
27+
Or you could search for `getItem` as most breaking changes are about its options.
2728

2829
## The bad part: breaking changes
2930

@@ -41,12 +42,17 @@ Also, `JSONSchemaNull` is removed.
4142

4243
Before v8, `type` was optional:
4344
```typescript
44-
this.localStorage.getItem('test', { schema: { items: { type: 'string' } } })
45+
this.localStorage.getItem('test', { schema: {
46+
items: { type: 'string' }
47+
} })
4548
```
4649

4750
Since v8:
4851
```typescript
49-
this.localStorage.getItem('test', { schema: { type: 'array', items: { type: 'string' } } })
52+
this.localStorage.getItem('test', {
53+
type: 'array',
54+
items: { type: 'string' }
55+
})
5056
```
5157

5258
Also, `items` no longer accepts an array of JSON schemas, meaning arrays with multiple types
@@ -58,16 +64,21 @@ are no longer possible (and it's better for consistency, use an object if you mi
5864

5965
Before v8, `type` was optional:
6066
```typescript
61-
this.localStorage.getItem('test', { schema: { properties: {
62-
test: { type: 'string' }
63-
} } })
67+
this.localStorage.getItem('test', { schema: {
68+
properties: {
69+
test: { type: 'string' }
70+
}
71+
} })
6472
```
6573

6674
Since v8:
6775
```typescript
68-
this.localStorage.getItem('test', { schema: { type: 'object', properties: {
69-
test: { type: 'string' }
70-
} } })
76+
this.localStorage.getItem('test', {
77+
type: 'object',
78+
properties: {
79+
test: { type: 'string' }
80+
}
81+
})
7182
```
7283

7384
### Validation of constants
@@ -82,7 +93,7 @@ this.localStorage.getItem('test', { schema: { const: 'value' } })
8293

8394
Since v8:
8495
```typescript
85-
this.localStorage.getItem('test', { schema: { type: 'string', const: 'value' } })
96+
this.localStorage.getItem('test', { type: 'string', const: 'value' })
8697
```
8798

8899
Also, `JSONSchemaConst` interface is removed.
@@ -99,7 +110,7 @@ this.localStorage.getItem('test', { schema: { enum: ['value 1', 'value 2'] } })
99110

100111
Since v8:
101112
```typescript
102-
this.localStorage.getItem('test', { schema: { type: 'string', enum: ['value 1', 'value 2'] } })
113+
this.localStorage.getItem('test', { type: 'string', enum: ['value 1', 'value 2'] })
103114
```
104115

105116
It also means enums of different types are no longer possible (and it's better for consistency).
@@ -119,15 +130,28 @@ While not recommended, you can still force it:
119130
this.localStorage.getItem('test', { schema: someSchema as any })
120131
```
121132

122-
123133
## The good part: simplication changes
124134

125-
The following changes are not required. Previous code will still work,
126-
but **for new code, follow these new guidelines**.
135+
The following changes are not required but strongly recommended.
136+
**Previous code will still work**, but *for new code, follow these new guidelines*.
137+
138+
### Easier API for `getItem()`
139+
140+
`getItem()` API has been simplified: the secong argument is now directly the schema for validation.
141+
142+
Before v8:
143+
```typescript
144+
this.localStorage.getItem<string>('test', { schema: { type: 'string' } })
145+
```
146+
147+
Since v8:
148+
```typescript
149+
this.localStorage.getItem('test', { type: 'string' })
150+
```
127151

128152
### Cast now inferred!
129153

130-
The returned type of `getItem()` is now inferred for basic types (`string`, `number`, `boolean`)
154+
The previous change allows that the returned type of `getItem()` is now inferred for basic types (`string`, `number`, `boolean`)
131155
and arrays of basic types (`string[]`, `number[]`, `boolean[]`).
132156

133157
Before v8:
@@ -143,21 +167,21 @@ this.localStorage.getItem<string>('test', { schema: { type: 'string' } }).subscr
143167

144168
Since v8:
145169
```typescript
146-
this.localStorage.getItem('test', { schema: { type: 'string' } }).subscribe((data) => {
170+
this.localStorage.getItem('test', { type: 'string' }).subscribe((data) => {
147171
data; // string :D
148172
});
149173
```
150174

151-
Cast is still needed for objects. Follow the [validation guide](./VALIDATION.md).
175+
Cast is still needed for objects. Follow the new [validation guide](./VALIDATION.md).
152176

153177
### Use the generic `JSONSchema`
154178

155-
Now the `JSONSchema` interface has been refactored, just use this one,
156-
and avoid the specific ones (`JSONSchemaString`, `JSONSchemaBoolean` and so on).
179+
Now the `JSONSchema` interface has been refactored, just use this one.
157180
IntelliSense will adjust itself based on the `type` option.
181+
The specific interfaces (`JSONSchemaString`, `JSONSchemaBoolean` and so on) are still there but useless.
158182

159183
`JSONSchemaNumeric` still works but is deprecated in favor of `JSONSchemaNumber` or `JSONSchemaInteger`
160-
(but again, just use `JSONSchema`)
184+
(but again, just use `JSONSchema`).
161185

162186
## More documentation
163187

0 commit comments

Comments
 (0)