-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
675 additions
and
30,771 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import NativeRobosats from './index' | ||
|
||
declare global { | ||
interface Window { | ||
ReactNativeWebView?: ReactNativeWebView | ||
NativeRobosats?: NativeRobosats | ||
} | ||
} | ||
|
||
export interface ReactNativeWebView { | ||
postMessage(message: string): void | ||
} | ||
|
||
export interface NativeWebViewMessageHttp { | ||
id?: number | ||
category: 'http' | ||
type: 'post' | 'get' | 'put' | 'delete' | ||
path: string | ||
headers?: object | ||
body?: object | ||
} | ||
|
||
export declare type NativeWebViewMessage = NativeWebViewMessageHttp | ||
|
||
export interface NativeRobosatsPromise { | ||
resolve: (value: object | PromiseLike<object>) => void, | ||
reject: (reason?: any) => void | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { NativeRobosatsPromise, NativeWebViewMessage } from './index.d' | ||
|
||
class NativeRobosats { | ||
constructor() { | ||
this.messageCounter = 0 | ||
} | ||
|
||
public messageCounter: number | ||
|
||
public pendingMessages: {[id:number]: NativeRobosatsPromise} = [] | ||
|
||
public onMessageResolve: (messageId: number, response?: object) => void = (messageId, response = {}) =>{ | ||
if (this.pendingMessages[messageId]) { | ||
this.pendingMessages[messageId].resolve(response) | ||
delete this.pendingMessages[messageId] | ||
} | ||
} | ||
|
||
public onMessageReject: (messageId: number, response?: object) => void = (messageId, response = {}) =>{ | ||
if (this.pendingMessages[messageId]) { | ||
this.pendingMessages[messageId].reject(response) | ||
delete this.pendingMessages[messageId] | ||
} | ||
} | ||
|
||
public postMessage: (message: NativeWebViewMessage) => Promise<object> = (message) => { | ||
this.messageCounter += 1 | ||
message.id = this.messageCounter | ||
const json = JSON.stringify(message) | ||
window.ReactNativeWebView?.postMessage(json) | ||
|
||
return new Promise<object>(async (resolve, reject) => { | ||
if (message.id) { | ||
this.pendingMessages[message.id] = { | ||
resolve: resolve, | ||
reject: reject | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export default NativeRobosats |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ApiClient } from '../api'; | ||
import { getCookie } from '../../../utils/cookies'; | ||
import NativeRobosats from '../../Native'; | ||
|
||
class ApiNativeClient implements ApiClient { | ||
constructor() { | ||
window.NativeRobosats = new NativeRobosats() | ||
} | ||
|
||
private readonly getHeaders: () => HeadersInit = () => { | ||
return { 'Content-Type': 'application/json', 'X-CSRFToken': getCookie('csrftoken') || '' }; | ||
}; | ||
|
||
public put: (path: string, body: object) => Promise<object | undefined> = async (path, body) => { | ||
const requestOptions = { | ||
method: 'PUT', | ||
headers: this.getHeaders(), | ||
body: JSON.stringify(body), | ||
}; | ||
return await fetch('https://unsafe.robosats.com' + path, requestOptions).then(async (response) => await response.json()); | ||
}; | ||
|
||
public delete: (path: string) => Promise<object | undefined> = async (path) => { | ||
const requestOptions = { | ||
method: 'DELETE', | ||
headers: this.getHeaders(), | ||
}; | ||
return await fetch('https://unsafe.robosats.com' + path, requestOptions).then(async (response) => await response.json()); | ||
}; | ||
|
||
public post: (path: string, body: object) => Promise<object | undefined> = async (path, body) => { | ||
return window.NativeRobosats?.postMessage({ | ||
category: 'http', | ||
type: 'post', | ||
path, | ||
body, | ||
headers: this.getHeaders() | ||
}) | ||
}; | ||
|
||
public get: (path: string) => Promise<object | undefined> = async (path) => { | ||
return window.NativeRobosats?.postMessage({ | ||
category: 'http', | ||
type: 'get', | ||
path | ||
}) | ||
}; | ||
} | ||
|
||
export default ApiNativeClient; |
Oops, something went wrong.