From c5db06f891e4595aca39c63eb13286de9df72b6b Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Thu, 11 Feb 2021 10:58:51 +0100 Subject: [PATCH 1/3] Update react-native-bridge to support POST requests (iOS) --- packages/react-native-bridge/index.js | 8 +++++-- .../ios/GutenbergBridgeDelegate.swift | 12 +++++++++-- .../ios/RNReactNativeGutenbergBridge.m | 3 ++- .../ios/RNReactNativeGutenbergBridge.swift | 16 ++++++++++++-- .../src/api-fetch-setup.js | 21 ++++++++++++++++--- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/packages/react-native-bridge/index.js b/packages/react-native-bridge/index.js index 1c4ee514d148a..82cfdac096df9 100644 --- a/packages/react-native-bridge/index.js +++ b/packages/react-native-bridge/index.js @@ -271,8 +271,12 @@ export function requestMediaEditor( mediaUrl, callback ) { ); } -export function fetchRequest( path ) { - return RNReactNativeGutenbergBridge.fetchRequest( path ); +export function fetchGetRequest( path ) { + return RNReactNativeGutenbergBridge.fetchGetRequest( path ); +} + +export function fetchPostRequest( path, data ) { + return RNReactNativeGutenbergBridge.fetchPostRequest( path, data ); } export function logUserEvent( event, properties ) { diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift b/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift index 5f05ec115d8a2..0e676e83ed773 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift @@ -201,13 +201,21 @@ public protocol GutenbergBridgeDelegate: class { /// func editorDidAutosave() - /// Tells the delegate that the editor needs to perform a network request. + /// Tells the delegate that the editor needs to perform a network GET request. /// The paths given to perform the request are from the WP ORG REST API. /// https://developer.wordpress.org/rest-api/reference/ /// - Parameter path: The path to perform the request. /// - Parameter completion: Completion handler to be called with the result or an error. - func gutenbergDidRequestFetch(path: String, completion: @escaping (Swift.Result) -> Void) + func gutenbergDidGetRequestFetch(path: String, completion: @escaping (Swift.Result) -> Void) + /// Tells the delegate that the editor needs to perform a network POST request. + /// The paths given to perform the request are from the WP ORG REST API. + /// https://developer.wordpress.org/rest-api/reference/ + /// - Parameter path: The path to perform the request. + /// - Parameter parameters: The parameters to be included in the request. + /// - Parameter completion: Completion handler to be called with the result or an error. + func gutenbergDidPostRequestFetch(path: String, parameters: [String: AnyObject]?, completion: @escaping (Swift.Result) -> Void) + /// Tells the delegate to display a fullscreen image from a given URL /// func gutenbergDidRequestImagePreview(with mediaUrl: URL, thumbUrl: URL?) diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m index 5ca99e956dc7f..0c7c488cdbf10 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m @@ -15,7 +15,8 @@ @interface RCT_EXTERN_MODULE(RNReactNativeGutenbergBridge, NSObject) RCT_EXTERN_METHOD(editorDidMount:(NSArray *)unsupportedBlockNames) RCT_EXTERN_METHOD(editorDidEmitLog:(NSString *)message logLevel:(int)logLevel) RCT_EXTERN_METHOD(editorDidAutosave) -RCT_EXTERN_METHOD(fetchRequest:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(fetchGetRequest:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(fetchPostRequest:(NSString *)path data:(NSDictionary *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(requestImageFullscreenPreview:(NSString *)currentImageUrlString originalImageUrlString:(NSString *)originalImageUrlString) RCT_EXTERN_METHOD(requestMediaEditor:(NSString *)mediaUrl callback:(RCTResponseSenderBlock)callback) RCT_EXTERN_METHOD(logUserEvent:(NSString *)event properties:(NSDictionary *)properties) diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift index 302187d23dbcc..22dbe86b6560c 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift @@ -237,8 +237,20 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter { } @objc - func fetchRequest(_ path: String, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) { - self.delegate?.gutenbergDidRequestFetch(path: path, completion: { (result) in + func fetchGetRequest(_ path: String, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) { + self.delegate?.gutenbergDidGetRequestFetch(path: path, completion: { (result) in + switch result { + case .success(let response): + resolver(response) + case .failure(let error): + rejecter("\(error.code)", error.description, error) + } + }) + } + + @objc + func fetchPostRequest(_ path: String, parameters: [String: AnyObject]?, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) { + self.delegate?.gutenbergDidPostRequestFetch(path: path, parameters: parameters, completion: { (result) in switch result { case .success(let response): resolver(response) diff --git a/packages/react-native-editor/src/api-fetch-setup.js b/packages/react-native-editor/src/api-fetch-setup.js index cbf34e78983c0..1e8c61b3cceff 100644 --- a/packages/react-native-editor/src/api-fetch-setup.js +++ b/packages/react-native-editor/src/api-fetch-setup.js @@ -1,7 +1,10 @@ /** * WordPress dependencies */ -import { fetchRequest } from '@wordpress/react-native-bridge'; +import { + fetchGetRequest, + fetchPostRequest, +} from '@wordpress/react-native-bridge'; import apiFetch from '@wordpress/api-fetch'; // Please add only wp.org API paths here! @@ -13,12 +16,24 @@ const SUPPORTED_ENDPOINTS = [ const setTimeoutPromise = ( delay ) => new Promise( ( resolve ) => setTimeout( resolve, delay ) ); -const fetchHandler = ( { path }, retries = 20, retryCount = 1 ) => { +const fetchHandler = ( + { path, data, method }, + retries = 20, + retryCount = 1 +) => { if ( ! isPathSupported( path ) ) { return Promise.reject( `Unsupported path: ${ path }` ); } - const responsePromise = fetchRequest( path ); + let responsePromise; + switch ( method ) { + case 'POST': + responsePromise = fetchPostRequest( path, data ); + break; + default: + responsePromise = fetchGetRequest( path ); + break; + } const parseResponse = ( response ) => { if ( typeof response === 'string' ) { From fe25b6884835432e00c4e14212ae5c6db93c35e5 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Thu, 11 Feb 2021 16:07:52 +0100 Subject: [PATCH 2/3] Update GutenbergDemo to support POST requests (iOS) --- .../ios/GutenbergDemo/GutenbergViewController.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift b/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift index 8bc821846f416..09c9bd9fa0d38 100644 --- a/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift +++ b/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift @@ -52,7 +52,11 @@ class GutenbergViewController: UIViewController { } extension GutenbergViewController: GutenbergBridgeDelegate { - func gutenbergDidRequestFetch(path: String, completion: @escaping (Result) -> Void) { + func gutenbergDidGetRequestFetch(path: String, completion: @escaping (Result) -> Void) { + completion(Result.success([:])) + } + + func gutenbergDidPostRequestFetch(path: String, parameters: [String: AnyObject]?, completion: @escaping (Result) -> Void) { completion(Result.success([:])) } From 3aed3df031410e7b977cf518b9b1d875436946f2 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Fri, 12 Feb 2021 17:48:36 +0100 Subject: [PATCH 3/3] Update fetchPostRequest parameter --- packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m index 0c7c488cdbf10..6d98a518c4773 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m @@ -16,7 +16,7 @@ @interface RCT_EXTERN_MODULE(RNReactNativeGutenbergBridge, NSObject) RCT_EXTERN_METHOD(editorDidEmitLog:(NSString *)message logLevel:(int)logLevel) RCT_EXTERN_METHOD(editorDidAutosave) RCT_EXTERN_METHOD(fetchGetRequest:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(fetchPostRequest:(NSString *)path data:(NSDictionary *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(fetchPostRequest:(NSString *)path parameters:(NSDictionary *)parameters resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(requestImageFullscreenPreview:(NSString *)currentImageUrlString originalImageUrlString:(NSString *)originalImageUrlString) RCT_EXTERN_METHOD(requestMediaEditor:(NSString *)mediaUrl callback:(RCTResponseSenderBlock)callback) RCT_EXTERN_METHOD(logUserEvent:(NSString *)event properties:(NSDictionary *)properties)