Skip to content

Commit

Permalink
fix: udpates
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Aug 30, 2024
1 parent 1e5f468 commit 154d3cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/npm/send/src/Send.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export interface SendError {
}

export interface SendResult {
response?: Response
error?: SendError
response: Response | undefined
error: SendError | undefined
}

export interface Send extends HybridObject<{ ios: 'swift' }> {
Expand Down
54 changes: 45 additions & 9 deletions packages/npm/send/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { NitroModules } from 'react-native-nitro-modules'
import {
Code,
type Request as _Request,
type Response as _Response,
type SendError as _SendError,
type Send,
type SendResult,
} from './Send.nitro'
export type Request = _Request & { method: Method }

export type SendRequest = _Request & { method: Method }
export type SendResponse = _Response
export type SendError = _SendError
export type SendResult =
| (_Response & { result: 'success' })
| (SendError & { result: 'error' })

export type Method =
| 'GET'
Expand All @@ -21,16 +28,45 @@ export type Method =

const Send = NitroModules.createHybridObject<Send>('Send')

export async function send(request: Request): Promise<SendResult> {
export async function send(request: SendRequest): Promise<SendResult> {
if (Platform.OS === 'ios') {
return Send.send(request)
try {
const result = await Send.send(request)
if (result.error !== undefined && result.response === undefined) {
return {
result: 'error',
code: result.error.code,
message: result.error.message,
}
} else if (result.response !== undefined && result.error === undefined) {
return {
result: 'success',
body: result.response.body,
header: result.response.header,
statusCode: result.response.statusCode,
}
} else {
return {
result: 'error',
code: Code.UNEXPECTED,
message: 'Unexpected error was thrown.',
}
}
} catch (error) {
return {
result: 'error',
code: Code.NO_RESPONSE,
message:
error instanceof Error
? error.message
: 'Unexpected error was thrown.',
}
}
} else {
return {
error: {
code: Code.NO_RESPONSE,
message: 'Platform is not supported',
},
response: undefined,
result: 'error',
code: Code.NO_RESPONSE,
message: 'Platform is not supported',
}
}
}

0 comments on commit 154d3cd

Please sign in to comment.