$pinc
4 min
The $pinc library gives you access to some common functions used across Pathfix. Below are the list of common scenarios where you might use the library.
Get the Pathfix API Call URL or Revoke URL
Syntax
$pinc.getPathfixUrl(providerName, publicKey, userId)
$pinc.getPathfixRevokeUrl(providerName, publicKey, userId)
Remarks
Arguments | Description |
|---|---|
providerName | Internal provider name. See Provider reference |
publicKey | optional: Defaults to the public key specified in the data-public-key attribute of the script tag |
userId | optional: Defaults to the data-user-id attribute of the script tag |
Example
//The code uses jQuery
function postMessage(){
//Request Pathfix to build the API call url
var url = $pinc.getPathfixUrl("googlecalendar");
var pfxPayload = {
"url":"https://www.googleapis.com/calendar/v3/users/me/calendarList",
"method":"GET"
}
$post(url, JSON.stringify(pfxPayload)).always(response=>{
console.log(response);
})
}
function disconnect(){
//Request Pathfix to build the User Disconnect/Revoke authorization url
var url = $pinc.getPathfixRevokeUrl("googlecalendar");
//no payload is needed
$post(url).always(response=>{
console.log(response);
})
}
Read Contents of a file before uploading to Online Storage
Syntax
$pinc.fileRead(FileReader, callback)
Returns
{"type": "HTTP_MIME_TYPE","content": "file_content", uploadObject:{}}
Remarks
Method Call
- FileReader - required, Javascript FileReader Object
- callback- callback function reference
Return
- content - The raw file content.
- uploadObject - Pass as a parameter to the payload when uploading the file using Pathfix passthrough Endpoints
Example
// fileInput is an HTML input element: <input type="file" id="myfileinput" multiple>
var fileInput = document.getElementById("myfileinput");
// files is a FileList object (similar to NodeList)
var files = fileInput.files;
var file;
// loop through files
for (var i = 0; i < files.length; i++) {
// get item
file = files.item(i);
//or
file = files[i];
//PATHFIX USAGE SAMPLE
var pfxFile = $pinc.fileRead(file,response => {
var pfxPayload = {
"url": `https://storage.googleapis.com/upload/storage/v1/b/${options.bucket}/o/`,
"method": "POST",
"headers": {
"x-payload-type": "file"
},
"payload": response.uploadObject
}
_upload
});
}
Extended code from developer.mozilla.org