Skip to content

Commit 5c547b9

Browse files
author
Crhistian Ramirez
committed
💥 revert filters to work how they did previously
Found out that the filter types weren't being enforced unless inlined. Essentially rendering the feature useless. This is because object literals undergo a special treatement called "excess property checks", if a property that doesn't exist is added it throws an error. This is what we want, but only happens if you inline your object.
1 parent 57f6818 commit 5c547b9

87 files changed

Lines changed: 2611 additions & 11296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ Let's run through a couple scenarios and what the call will look like with the S
118118
My products where `xp.Featured` is `true`
119119

120120
```javascript
121-
Me.ListProducts({ filters: { xp: { Featured: true } } })
121+
Me.ListProducts({ filters: { 'xp.Featued': true } })
122122
.then(productList => console.log(productList));
123123
```
124124

125125
My orders submitted after April 20th, 2018
126126

127127
```javascript
128-
Me.ListOrders({ filters: { DateSubmitted: '>2019-04-20' } })
128+
Me.ListOrders({ filters: { DateSubmitted: '>2020-04-20' } })
129129
.then(orderList => console.log(orderList))
130130
```
131131

@@ -146,7 +146,7 @@ Users.List('my-mock-buyerid', { filters: { LastName: 'Smith*|*Jones' } })
146146
Products where xp.Color is not red *and* not blue
147147

148148
```javascript
149-
Products.List({ filters: { xp: { Color: ['!red', '!blue'] } } })
149+
Products.List({ filters: { 'xp.Color': ['!red', '!blue'] } })
150150
.then(productList => console.log(productList));
151151
```
152152

codegen/hooks.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ const postFormatOperation: PostFormatOperationHook = function(operation) {
3838
})
3939

4040
if (operation.isList) {
41-
// add ListPage and ListPageWithFacets to fileImports
41+
// add list models to file imports
4242
operation.fileImports = [
4343
operation.isFacetList ? 'ListPageWithFacets' : 'ListPage',
4444
'Searchable',
4545
'Sortable',
46+
'Filters',
4647
...operation.fileImports,
4748
]
4849
}
@@ -120,13 +121,7 @@ function findTypeForOperationProps(prop: Param, operation: Operation) {
120121
}
121122

122123
if (prop.name === 'filters') {
123-
// we're updating the behavior of filters so that we get better type inference
124-
// instead of accepting dot-referenced xp values such as { 'xp.color': 'red' }
125-
// we will now expect { xp: { color: 'red' } }
126-
prop['isFilter'] = true
127-
prop.description =
128-
'An object whose keys match the model, and the values are the values to filter by'
129-
return `Filters<Required<T${operation.returnType}>>`
124+
return `Filters`
130125
}
131126

132127
if (prop.isEnum && !prop.isCustomType) {

codegen/templates/api/_RESOURCE_.ts.hbs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { {{this}} } from '../models/{{this}}';
33
{{/resource.fileImports}}
44
import { PartialDeep } from '../models/PartialDeep';
55
import { RequiredDeep } from '../models/RequiredDeep';
6-
import { Filters } from '../models/Filters';
76
import { RequestOptions } from '../models/RequestOptions';
87
import http from '../utils/HttpClient';
98
import OrderCloudError from '../utils/OrderCloudError';
@@ -31,12 +30,10 @@ class {{resource.id}} {
3130
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
3231
* @param requestOptions.requestType Provide a value that can be used to identify the type of request. Useful for error logs.
3332
*/
34-
public async {{name}}({{#allParams}}{{#unless isQueryParam}}{{#if isBodyParam}}{{#if ../isPatch}}{{name}}: PartialDeep<{{typescriptType}}>, {{else}}{{name}}: {{typescriptType}},{{/if}}{{else}}{{name}}: {{typescriptType}}, {{/if}}{{/unless}}{{/allParams}}{{#if hasQueryParams}}listOptions?: { {{#queryParams}}{{#unless isFilter}}{{name}}?: {{typescriptType}}{{/unless}}{{#if isFilter}}filters?: Filters<Required<{{../returnType}}>>{{/if}}{{#unless @last}}, {{/unless}}{{/queryParams}} }, {{/if}}requestOptions?: RequestOptions ): Promise<{{#if hasReturnType}}RequiredDeep<{{#if isList}}{{#if isFacetList}}ListPageWithFacets<{{returnType}}>{{else}}ListPage<{{returnType}}>{{/if}}{{else}}{{returnType}}{{/if}}>{{else}}void{{/if}}>;
35-
public async {{name}}{{#if hasReturnType}}{{#if isList}}<T{{returnType}} extends {{returnType}}>{{else}}<T{{returnType}} extends {{returnType}}>{{/if}}{{/if}}({{#allParams}}{{#unless isQueryParam}}{{#if isBodyParam}}{{#if ../isPatch}}{{name}}: PartialDeep<{{typescriptType}}>, {{else}}{{name}}: {{typescriptType}},{{/if}}{{else}}{{name}}: {{typescriptType}}, {{/if}}{{/unless}}{{/allParams}}{{#if hasQueryParams}}listOptions?: { {{#queryParams}}{{#unless isFilter}}{{name}}?: {{typescriptType}}{{/unless}}{{#if isFilter}}filters?: Filters<Required<T{{../returnType}}>>{{/if}}{{#unless @last}}, {{/unless}}{{/queryParams}} }, {{/if}}requestOptions?: RequestOptions ): Promise<{{#if hasReturnType}}RequiredDeep<{{#if isList}}{{#if isFacetList}}ListPageWithFacets<T{{returnType}}>{{else}}ListPage<T{{returnType}}>{{/if}}{{else}}T{{returnType}}{{/if}}>{{else}}void{{/if}}>;
36-
public async {{name}}{{#if hasReturnType}}{{#if isList}}<T{{returnType}} extends {{returnType}}>{{else}}<T{{returnType}} extends {{returnType}}>{{/if}}{{/if}}({{#allParams}}{{#unless isQueryParam}}{{#if isBodyParam}}{{#if ../isPatch}}{{name}}: PartialDeep<{{typescriptType}}>, {{else}}{{name}}: {{typescriptType}},{{/if}}{{else}}{{name}}: {{typescriptType}}, {{/if}}{{/unless}}{{/allParams}}{{#if hasQueryParams}}listOptions: { {{#queryParams}}{{#unless isFilter}}{{name}}?: {{typescriptType}}{{/unless}}{{#if isFilter}}filters?: Filters<Required<T{{../returnType}}>>{{/if}}{{#unless @last}}, {{/unless}}{{/queryParams}} } = {}, {{/if}}requestOptions: RequestOptions = {} ): Promise<{{#if hasReturnType}}RequiredDeep<{{#if isList}}{{#if isFacetList}}ListPageWithFacets<T{{returnType}}>{{else}}ListPage<T{{returnType}}>{{/if}}{{else}}T{{returnType}}{{/if}}>{{else}}void{{/if}}>{
33+
public async {{name}}{{#if hasReturnType}}{{#if isList}}<T{{returnType}} extends {{returnType}}>{{else}}<T{{returnType}} extends {{returnType}}>{{/if}}{{/if}}({{#allParams}}{{#unless isQueryParam}}{{#if isBodyParam}}{{#if ../isPatch}}{{name}}: PartialDeep<{{typescriptType}}>, {{else}}{{name}}: {{typescriptType}},{{/if}}{{else}}{{name}}: {{typescriptType}}, {{/if}}{{/unless}}{{/allParams}}{{#if hasQueryParams}}listOptions: { {{#queryParams}}{{name}}?: {{typescriptType}}{{#unless @last}}, {{/unless}}{{/queryParams}} } = {}, {{/if}}requestOptions: RequestOptions = {} ): Promise<{{#if hasReturnType}}RequiredDeep<{{#if isList}}{{#if isFacetList}}ListPageWithFacets<T{{returnType}}>{{else}}ListPage<T{{returnType}}>{{/if}}{{else}}T{{returnType}}{{/if}}>{{else}}void{{/if}}>{
3734
const impersonating = this.impersonating;
3835
this.impersonating = false;
39-
return await http.{{verb}}(`{{parameterizePath path}}`, { ...requestOptions, {{#if hasBodyParam}}data: {{bodyParam.name}}, {{/if}}impersonating, params: { {{#if hasQueryParams}}...listOptions, {{/if}} {{#if hasFilters}}filters: listOptions.filters, {{/if}} } } )
36+
return await http.{{verb}}(`{{parameterizePath path}}`, { ...requestOptions, {{#if hasBodyParam}}data: {{bodyParam.name}}, {{/if}}impersonating, {{#if hasQueryParams}}params: listOptions {{/if}} } )
4037
.catch(ex => {
4138
if(ex.response) {
4239
throw new OrderCloudError(ex)

codegen/templates/models/Filters.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@
44
* ex: an order list call with { xp: { hasPaid: '!*' } } would return a list of orders where xp.hasPaid is null or undefined
55
* https://ordercloud.io/features/advanced-querying#filtering
66
*/
7-
type FilterProp = string | string[] | boolean | undefined
8-
type Primitive = string | boolean | number
7+
type FilterProp = string | string[] | number | boolean | undefined
98

109
/**
1110
* Used on list calls to filter a list of items
1211
* https://ordercloud.io/features/advanced-querying#filtering
1312
*/
14-
export type Filters<T> = T extends object
15-
? FiltersObj<T>
16-
: T extends Primitive
17-
? FilterProp
18-
: any
1913

20-
type FiltersObj<T extends object> = {
21-
-readonly [prop in keyof T]?: Filters<T[prop]>
14+
export type Filters = {
15+
[key: string]: FilterProp
2216
}

docs/assets/js/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)