Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changed fetch parameters in the adapter
  • Loading branch information
priandsf committed Mar 15, 2020
1 parent 8372763 commit 4f96c99
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 9 additions & 4 deletions packages/fetch/README.md
Expand Up @@ -98,7 +98,7 @@ If the URL is more complex to build, then it can be defined as a component prope
url: '$url',
...
}) mydata;
```
```


### Fetch options (init)
Expand Down Expand Up @@ -127,14 +127,19 @@ To support that, the wire adapter offers a `lazy` mode. When set to true, the re
This fetch method has the following signature:

```javascript
fetch(init?: RequestInit, queryParams?: Record<string, any>, variables?: Record<string, any>): Promise<void>
interface FetchParams {
init?: RequestInit,
queryParams?: Record<string, any>,
variables?: Record<string, any>
};
fetch(params?: FetchParams): Promise<void>
```
- `init`
These are initialization parameters that are merged with the ones defined at the adapter level.
- `queryParams`
Replaces, if defined, the query parameters defined at the wire adapter level.
Merges, if defined, with the query parameters defined at the wire adapter level.
- `variables`
Replaces, if defined, the variables defined at the wire adapter level.
Merges, if defined, with the variables defined at the wire adapter level.

The function returns a `Promise` that can be observed to know when the result has been retrieved. The `Promise` does not provide that value, but it can be accessed from the @wire variable.

Expand Down
17 changes: 12 additions & 5 deletions packages/fetch/src/fetch/usefetch.ts
Expand Up @@ -5,17 +5,22 @@
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { register, ValueChangedEvent } from '@lwc/wire-service';

import { FetchClient, getFetchClient } from './client';

interface FetchParams {
init?: RequestInit,
queryParams?: Record<string, any>,
variables?: Record<string, any>
};

interface Result {
loading: boolean;
data?: any;
error?: string;
initialized: boolean;

client: FetchClient;
fetch?: (options?: RequestInit, q?: Record<string, any>|undefined, v?: Record<string, any>|undefined) => Promise<void>;
fetch?: ( params?: FetchParams ) => Promise<void>;
}

//
Expand Down Expand Up @@ -49,14 +54,16 @@ register(useFetch, eventTarget => {
}
}

function fetch(options?: RequestInit, qp?: Record<string, any>|undefined, v?: Record<string, any>|undefined): Promise<void> {
function fetch( params?: FetchParams ): Promise<void> {
if(url) {
const _init = {...init, ...options};
const _init = {...init, ...(params && params.init) };
const _queryParams= {...queryParams, ...(params && params.queryParams) };
const _variables = {...variables, ...(params && params.variables) };
pendingResult.loading = true;
const fetchIndex=++fetchCurrent;
update();
try {
return pendingResult.client.fetch(url,_init,qp||queryParams,v||variables).then( (response) => {
return pendingResult.client.fetch(url,_init,_queryParams,_variables).then( (response) => {
return response.json();
}).then( (json: any) => {
if(fetchIndex==fetchCurrent) {
Expand Down
2 changes: 1 addition & 1 deletion packages/sample-app/src/fetch/users/users.js
Expand Up @@ -88,7 +88,7 @@ export default class Users extends LightningElement {
handleUserClick(event) {
const idx = this._findUserIdx(event);
if(idx!==undefined) {
this.userById.fetch(undefined,undefined,{userId:this.users.data.users[idx].email}).then( () => {
this.userById.fetch({variables:{userId:this.users.data.users[idx].email}}).then( () => {
// eslint-disable-next-line no-alert
alert(JSON.stringify(this.userById.data));
});
Expand Down

0 comments on commit 4f96c99

Please sign in to comment.