Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Add support for POST requests #28938

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/react-native-bridge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
12 changes: 10 additions & 2 deletions packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any, NSError>) -> Void)
func gutenbergDidGetRequestFetch(path: String, completion: @escaping (Swift.Result<Any, NSError>) -> 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<Any, NSError>) -> Void)

/// Tells the delegate to display a fullscreen image from a given URL
///
func gutenbergDidRequestImagePreview(with mediaUrl: URL, thumbUrl: URL?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class GutenbergViewController: UIViewController {
}

extension GutenbergViewController: GutenbergBridgeDelegate {
func gutenbergDidRequestFetch(path: String, completion: @escaping (Result<Any, NSError>) -> Void) {
func gutenbergDidGetRequestFetch(path: String, completion: @escaping (Result<Any, NSError>) -> Void) {
completion(Result.success([:]))
}

func gutenbergDidPostRequestFetch(path: String, parameters: [String: AnyObject]?, completion: @escaping (Result<Any, NSError>) -> Void) {
completion(Result.success([:]))
}

Expand Down
21 changes: 18 additions & 3 deletions packages/react-native-editor/src/api-fetch-setup.js
Original file line number Diff line number Diff line change
@@ -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!
Expand All @@ -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' ) {
Expand Down