Skip to content

Commit 9c0f2ff

Browse files
committed
feat: ios - add request and response observer methods for debugging in RNSslPinning
1 parent 5d4a827 commit 9c0f2ff

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

ios/RNSslPinning/RNSslPinning.m

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#import "RNSslPinning.h"
55
#import "AFNetworking.h"
66

7+
static void (^_requestObserver)(NSURLRequest *) = nil;
8+
static void (^_responseObserver)(NSURLRequest *, NSHTTPURLResponse *, NSData *, NSTimeInterval) = nil;
9+
710
@interface RNSslPinning()
811

912
@property (nonatomic, strong) NSURLSessionConfiguration *sessionConfig;
@@ -13,6 +16,18 @@ @interface RNSslPinning()
1316
@implementation RNSslPinning
1417
RCT_EXPORT_MODULE();
1518

19+
+ (void)setRequestObserver:(void (^)(NSURLRequest *))observer {
20+
#if DEBUG
21+
_requestObserver = [observer copy];
22+
#endif
23+
}
24+
25+
+ (void)setResponseObserver:(void (^)(NSURLRequest *, NSHTTPURLResponse *, NSData *, NSTimeInterval))observer {
26+
#if DEBUG
27+
_responseObserver = [observer copy];
28+
#endif
29+
}
30+
1631
- (instancetype)init
1732
{
1833
self = [super init];
@@ -65,14 +80,44 @@ - (instancetype)init
6580

6681

6782
-(void)performRequest:(AFURLSessionManager*)manager obj:(NSDictionary *)obj request:(NSMutableURLRequest*) request callback:(RCTResponseSenderBlock) callback {
68-
83+
#if DEBUG
84+
if (_requestObserver) {
85+
_requestObserver(request);
86+
}
87+
#endif
88+
89+
NSURLRequest *capturedRequest = [request copy]; // 🧠 Save the original request - for interceptors purposes
90+
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970] * 1000.0;
91+
92+
6993
[[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
70-
71-
7294
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
7395
NSString *bodyString = [[NSString alloc] initWithData: responseObject encoding:NSUTF8StringEncoding];
7496
NSInteger statusCode = httpResp.statusCode;
7597

98+
// Don't create a synthetic response - pass the real one to observer along with error
99+
if (error && (!httpResp || httpResp.statusCode == 0)) {
100+
bodyString = error.localizedDescription;
101+
}
102+
103+
#if DEBUG
104+
if (_responseObserver) {
105+
NSData *rawData = nil;
106+
if (responseObject) {
107+
rawData = [responseObject isKindOfClass:[NSData class]]
108+
? responseObject
109+
: [NSJSONSerialization dataWithJSONObject:responseObject options:0 error:nil];
110+
} else if (error) {
111+
// Create error response data if we have an error but no response data
112+
NSString *errorMessage = error.localizedDescription ?: @"Unknown error";
113+
rawData = [errorMessage dataUsingEncoding:NSUTF8StringEncoding];
114+
}
115+
116+
// Pass the raw error to our observer with the start time
117+
_responseObserver(capturedRequest, httpResp, rawData ?: [NSData new], startTime);
118+
}
119+
#endif
120+
76121
if (!error) {
77122
// if(obj[@"responseType"]){
78123
NSString * responseType = obj[@"responseType"];

0 commit comments

Comments
 (0)