Last active
September 10, 2019 21:30
-
-
Save dabit3/5061789aa0565410b043d3ff96351555 to your computer and use it in GitHub Desktop.
Various changes to fix Predictions category for AWS Amplify React Native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Utils.js | |
// new util for RN File Source | |
export async function fileToRNArrayBuffer(uri) { | |
return new Promise((res, reject) => { | |
var request = new XMLHttpRequest(); | |
request.open("GET", uri, true); | |
request.responseType = "arraybuffer"; | |
request.onload = _event => { res(request.response) } | |
request.onerror = err => { reject(err); }; | |
request.send(null); | |
}); | |
} | |
// AmazonAIIdentifyPredictionsProvider.js | |
// update imports | |
import { | |
IdentifyLabelsInput, IdentifyLabelsOutput, IdentifySource, IdentifyEntitiesInput, IdentifyEntitiesOutput, | |
isStorageSource, isFileSource, isRNFileSource, isBytesSource, IdentifyTextInput, IdentifyTextOutput, isIdentifyCelebrities, | |
isIdentifyFromCollection, IdentifyFromCollection | |
} from '../types'; | |
import { makeCamelCase, makeCamelCaseArray, blobToArrayBuffer, fileToRNArrayBuffer } from './Utils'; | |
// check if is RNFilesource | |
else if (isRNFileSource(source)) { | |
fileToRNArrayBuffer(source.file.uri) | |
.then(buffer => { res({ Bytes: buffer }) }) | |
.catch(err => rej(err)) | |
} | |
// types/Predictions.js | |
// New types for RNFileSource | |
export type IdentifySource = StorageSource | FileSource | BytesSource | RNFileSource; | |
export function isRNFileSource(obj: any): obj is RNFileSource { | |
const key: keyof RNFileSource = 'file'; | |
const uri: keyof RNFile = 'uri'; | |
return obj && obj.hasOwnProperty(key) && obj[key].hasOwnProperty(uri); | |
} | |
export interface RNFileSource { | |
file: RNFile | |
} | |
export interface RNFile { | |
uri: string | |
width: number | |
height: number | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment