我想测试一个需要环境变数的终点。
rror: ❌试图进入客户的服务器边环境变量。
getHash src/services/api.ts:7:23
5| const getTimestamp = () => Date.now().toString();
6| const getHash = (timeStamp: string) =>
7| md5(timeStamp + env.MARVEL_API_PRIVATE_KEY + env.MARVEL_API_PUBLIC_KEY);
这是我迄今所做的:
import md5 from md5 ;
import { env } from @/env ;
const getTimestamp = () => Date.now().toString();
const getHash = (timeStamp: string) =>
md5(timeStamp + env.MARVEL_API_PRIVATE_KEY + env.MARVEL_API_PUBLIC_KEY);
const timeStamp = getTimestamp();
const hash = getHash(timeStamp);
const query = `ts=${timeStamp}&apikey=${env.MARVEL_API_PUBLIC_KEY}&hash=${hash}`;
export const getCharacters = async () => {
const url = `${env.MARVEL_API_URL}/characters?limit=50&${query}`;
const response = await fetch(url);
const data = response.json();
return data;
};
ms手:
import { HttpResponse, http } from msw ;
export const handlers = [
http.get( /characters , () => {
return HttpResponse.json(
{
id: 1011334,
name: 3-D Man ,
},
{ status: 200 },
);
}),
];