솔라나 cors 이슈 해결

00_8_3·2022년 6월 30일
0

솔라나

목록 보기
4/5

이유

@solana/web3.js의 Connection 함수를 사용 시
솔라나넷과의 통신에서 cors 오류가 발생.

구글을 찾아 보아도 해결방법이 나오지 않아서 코드를 뜯어 봤더니
Connection의 생성자 파라미터 중 ConnectionConfig에 fetch을 제공하면
Connection이 default로 사용하던 fetch를 커스텀 가능.

solana-client 헤더를 제거하여 cors 문제 해결.

const customFetch = (url: RequestInfo, init?: any) => {
	const { headers, ...rest} = init;
	const {"solana-client": _, ...headerRest} = headers;

	const option = init ? {
		...rest,
		headers: {
			...headerRest
		},
	} : undefined
	return fetch(url, option);
}

출처

https://solana-labs.github.io/solana-web3.js/classes/Connection.html#constructor

https://github.com/solana-labs/solana-web3.js/blob/6482d0d/src/connection.ts#L2426

https://github.com/solana-labs/solana-web3.js/blob/6482d0dc120cab140dd07b216247fdac1b8ddaa6/src/connection.ts#L1059

https://solana-labs.github.io/solana-web3.js/modules.html#ConnectionConfig

https://github.com/node-fetch/node-fetch/blob/main/%40types/index.d.ts

0개의 댓글