import { requestTokenSignature , accessTokenSignature } from "./signature" ; interface RequestTokenResponse { oauth_token : string ; oauth_token_secret : string ; oauth_callback_confirmed ? : string ; } const parseOAuthRequestToken = ( responseText ) => responseText . split ( "&" ) . reduce ( ( prev , el ) => { const [ key , value ] = el . split ( "=" ) ; return { ... prev , [ key ] : value } ; } , { } as RequestTokenResponse ) ; export const obtainOauthRequestToken = async ( { consumerKey , consumerSecret , callbackUrl , method , apiUrl } ) => { const oauthSignature = requestTokenSignature ( { method , apiUrl , callbackUrl , consumerKey , consumerSecret } ) ; const res = await fetch ( `https://cors.bridged.cc/${ apiUrl }` , { method , headers : { Authorization : `OAuth ${ oauthSignature }` } } ) ; const responseText = await res . text ( ) ; return parseOAuthRequestToken ( responseText ) ; } ; export const obtainOauthAccessToken = async ( { consumerKey , consumerSecret , oauthToken , oauthVerifier , method , apiUrl } ) => { const oauthSignature = accessTokenSignature ( { method , apiUrl , consumerKey , consumerSecret , oauthToken , oauthVerifier } ) ; const res = await fetch ( `https://cors.bridged.cc/${ apiUrl }` , { method , headers : { Authorization : `OAuth ${ oauthSignature }` } } ) ; const responseText = await res . text ( ) ; return parseOAuthRequestToken ( responseText ) ; } ;