Skip to content
This repository was archived by the owner on Jan 27, 2024. It is now read-only.

Commit 25ee870

Browse files
committed
added support for js fetch
replaced postmate with penpal v4.0.0
1 parent fff3b70 commit 25ee870

36 files changed

+1007
-267
lines changed

.forceignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44

55
package.xml
66
**/profiles
7-
**/applications/standard__*
7+
**/applications/standard__*
8+
9+
# LWC configuration files
10+
**/jsconfig.json
11+
**/.eslintrc.json
12+
13+
# LWC Jest
14+
**/__tests__/**

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Find the `<c:lc_api>` by its `aura:id` then call the `restRequest(..)` method pa
4444
createAccount: function( component, event, helper ) {
4545

4646
component.find( 'restClient' ).restRequest({
47-
'url' : '/services/data/v44.0/sobjects/Account',
47+
'url' : '/services/data/v45.0/sobjects/Account',
4848
'method' : 'post',
4949
'body' : JSON.stringify({
5050
"Name" : "LC Demo Account"

force-app/main/aura/LC_API/LC_API.cmp

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
<!--
22
Author: Doug Ayers
33
Website: https://douglascayers.com
4-
GitHub: https://github.com/douglascayers/sfdx-lightning-api
4+
GitHub: https://github.com/douglascayers/sfdx-lightning-api-component
55
License: BSD 3-Clause License
66
-->
7-
<aura:component controller="LC_APIController">
7+
<aura:component controller="LC_VisualforceDomainController">
88

9-
<ltng:require scripts="{!$Resource.postmate}"
9+
<ltng:require scripts="{!$Resource.penpal}"
1010
afterScriptsLoaded="{!c.onScriptsLoaded}"/>
1111

12+
<aura:attribute name="iframeSrc"
13+
type="String"
14+
access="private"
15+
description="Which visualforce page to load for bridging API calls."/>
16+
17+
<aura:attribute name="penpalInitialized"
18+
type="Boolean"
19+
access="private"
20+
default="false"
21+
description="Has the Penpal connection been initialized?"/>
22+
23+
<aura:handler name="init" value="{!this}" action="{!c.onInit}"/>
24+
25+
<aura:handler name="render" value="{!this}" action="{!c.onRender}"/>
26+
1227
<!--
13-
Makes REST API request and returns a promise that resolves to the response.
28+
Makes a Salesforce REST API request and returns a promise that resolves to the response.
1429

1530
@param request
1631
JSON object with properties:
@@ -24,7 +39,7 @@ License: BSD 3-Clause License
2439

2540
Example usage:
2641
component.find( 'lcAPI' ).restRequest({
27-
'url' : '/services/data/v44.0/sobjects/Account',
42+
'url' : '/services/data/v45.0/sobjects/Account',
2843
'method' : 'post',
2944
'body' : JSON.stringify({
3045
'Name' : 'Salesforce',
@@ -48,6 +63,70 @@ License: BSD 3-Clause License
4863
description="Supports these keys: url (string), method (string), body (string), headers (map)."/>
4964
</aura:method>
5065

51-
<div aura:id="postmate" class="slds-hide"></div>
66+
<!--
67+
Makes a JavaScript Fetch request and returns a promise that resolves to the response.
68+
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
69+
70+
@param request
71+
JSON object with properties:
72+
'url' (String, required) The url to fetch.
73+
'options' (Map, optional) The init options for the request.
74+
75+
Example usage:
76+
component.find( 'lcAPI' ).fetchRequest({
77+
'url' : 'https://example.com',
78+
'options': {
79+
'method' : 'GET',
80+
'headers' : {
81+
'Accepts' : 'application/json'
82+
}
83+
}
84+
}).then( $A.getCallback( function( response ) {
85+
// handle response
86+
})).catch( $A.getCallback( function( err ) {
87+
// handle error
88+
}));
89+
-->
90+
<aura:method name="fetchRequest" action="{!c.onFetchRequest}">
91+
<aura:attribute name="request"
92+
type="Map"
93+
required="true"
94+
description="Supports these keys: url (string), options (map)."/>
95+
</aura:method>
96+
97+
<div class="slds-hide">
98+
<iframe aura:id="penpalFrame" src="{!v.iframeSrc}"></iframe>
99+
</div>
52100

53101
</aura:component>
102+
<!--
103+
BSD 3-Clause License
104+
105+
Copyright (c) 2018, Doug Ayers, douglascayers.com
106+
All rights reserved.
107+
108+
Redistribution and use in source and binary forms, with or without
109+
modification, are permitted provided that the following conditions are met:
110+
111+
* Redistributions of source code must retain the above copyright notice, this
112+
list of conditions and the following disclaimer.
113+
114+
* Redistributions in binary form must reproduce the above copyright notice,
115+
this list of conditions and the following disclaimer in the documentation
116+
and/or other materials provided with the distribution.
117+
118+
* Neither the name of the copyright holder nor the names of its
119+
contributors may be used to endorse or promote products derived from
120+
this software without specific prior written permission.
121+
122+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
123+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
124+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
125+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
126+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
127+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
128+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
129+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
130+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
131+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
132+
-->
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>44.0</apiVersion>
3+
<apiVersion>45.0</apiVersion>
44
<description>LC_API</description>
55
</AuraDefinitionBundle>
Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,110 @@
11
/*
22
Author: Doug Ayers
33
Website: https://douglascayers.com
4-
GitHub: https://github.com/douglascayers/sfdx-lightning-api
4+
GitHub: https://github.com/douglascayers/sfdx-lightning-api-component
55
License: BSD 3-Clause License
66
*/
77
({
8-
onScriptsLoaded: function( component, event, helper ) {
9-
helper.makeApexRequest( component, 'c.getVisualforceDomainURL', {}, { storable: true } ).then( function( vfDomainURL ) {
10-
helper.handleOnPostmateScriptsLoaded(
11-
component,
12-
component.find( 'postmate' ).getElement(),
13-
`${vfDomainURL}/apex/LC_APIPage`
14-
);
15-
}).catch( $A.getCallback( function( err ) {
16-
console.error( 'LC_API: Error in script initialization', err );
8+
/**
9+
* Called once during component initialization phase.
10+
*/
11+
onInit: function( component, event, helper ) {
12+
helper._penpal = {};
13+
helper.makeApexRequest( component, 'c.getVisualforceDomainURL' ).then( $A.getCallback( function( vfDomainURL ) {
14+
component.set( 'v.iframeSrc', `${vfDomainURL}/apex/LC_APIPage` );
15+
})).catch( $A.getCallback( function( err ) {
16+
console.error( 'LC_API: Error determining visualforce domain', err );
1717
}));
1818
},
1919

20+
/**
21+
* Called once after ltng:require has loaded scripts.
22+
*/
23+
onScriptsLoaded: function( component, event, helper ) {
24+
25+
},
26+
27+
/**
28+
* Called each time the component renders itself.
29+
*/
30+
onRender: function( component, event, helper ) {
31+
32+
let initialized = component.get( 'v.penpalInitialized' );
33+
34+
// Since the iframe source is calculated asynchronously,
35+
// we listen to the component's render events and each time
36+
// check if the iframe is ready, and if so, then we initialize
37+
// penpal to connect this component to the iframe.
38+
// Since we only want to do this once, we also set the initialized flag.
39+
if ( !initialized ) {
40+
41+
let iframeElmt = component.find( 'penpalFrame' ).getElement();
42+
43+
if ( !$A.util.isEmpty( iframeElmt.src ) ) {
44+
45+
component.set( 'v.penpalInitialized', true );
46+
47+
const connection = Penpal.connectToChild({
48+
// The iframe to which a connection should be made
49+
iframe: iframeElmt
50+
});
51+
52+
helper._penpal.connection = connection;
53+
54+
connection.promise.then( $A.getCallback( function( child ) {
55+
// Cache a reference to the child so that we can
56+
// use it in the restRequest/fetchRequest methods,
57+
// as well as be able to destroy it when this component unrenders.
58+
helper._penpal.child = child;
59+
})).catch( $A.getCallback( function( err ) {
60+
console.error( 'LC_API: Error establishing connection to iframe', err );
61+
component.set( 'v.penpalInitialized', false );
62+
}));
63+
64+
} // else, iframe source is empty, keep waiting
65+
66+
}
67+
68+
},
69+
2070
onRestRequest: function( component, event, helper ) {
2171
var params = event.getParam( 'arguments' );
2272
return helper.handleRestRequest( component, params.request );
73+
},
74+
75+
onFetchRequest: function( component, event, helper ) {
76+
var params = event.getParam( 'arguments' );
77+
return helper.handleFetchRequest( component, params.request );
2378
}
24-
})
79+
})
80+
/*
81+
BSD 3-Clause License
82+
83+
Copyright (c) 2018, Doug Ayers, douglascayers.com
84+
All rights reserved.
85+
86+
Redistribution and use in source and binary forms, with or without
87+
modification, are permitted provided that the following conditions are met:
88+
89+
* Redistributions of source code must retain the above copyright notice, this
90+
list of conditions and the following disclaimer.
91+
92+
* Redistributions in binary form must reproduce the above copyright notice,
93+
this list of conditions and the following disclaimer in the documentation
94+
and/or other materials provided with the distribution.
95+
96+
* Neither the name of the copyright holder nor the names of its
97+
contributors may be used to endorse or promote products derived from
98+
this software without specific prior written permission.
99+
100+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
101+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
102+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
103+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
104+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
105+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
106+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
107+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
108+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
109+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110+
*/

0 commit comments

Comments
 (0)