T
TanStack3mo ago
conscious-sapphire

Server Functions

Hello, what is the best practice to initialize classes in server files with secret keys? Can I just initialize them outside the server functions or should I always do it inside it? Right now I'm doing it like so:
const GIPHY_API_KEY = process.env.GIPHY_API_KEY!;
const giphyFetch = new GiphyFetch(GIPHY_API_KEY); // is this safe to do outside?

const data = z.object({ offset: z.number().optional(), term: z.string() });

export const fetchGifs = createServerFn({ method: "POST" })
.validator(data)
.handler(async ({ data }): Promise<GifsResult> => {
const { offset, term } = data;
if (!GIPHY_API_KEY) {
throw new Error("GIPHY_API_KEY is not set");
}

if (term === "") {
console.log("fetchGifs called for trending with offset:", offset);
return giphyFetch.trending({ offset, limit: 10 });
}

return giphyFetch.search(term, { offset, limit: 10 });
});
const GIPHY_API_KEY = process.env.GIPHY_API_KEY!;
const giphyFetch = new GiphyFetch(GIPHY_API_KEY); // is this safe to do outside?

const data = z.object({ offset: z.number().optional(), term: z.string() });

export const fetchGifs = createServerFn({ method: "POST" })
.validator(data)
.handler(async ({ data }): Promise<GifsResult> => {
const { offset, term } = data;
if (!GIPHY_API_KEY) {
throw new Error("GIPHY_API_KEY is not set");
}

if (term === "") {
console.log("fetchGifs called for trending with offset:", offset);
return giphyFetch.trending({ offset, limit: 10 });
}

return giphyFetch.search(term, { offset, limit: 10 });
});
4 Replies
breakable-bronze
breakable-bronze3mo ago
looks good
conscious-sapphire
conscious-sapphireOP3mo ago
aight thank you!
absent-sapphire
absent-sapphire2mo ago
I would even go and abstract that into own serverOnlyFunction, for making 100% sure that GIPHY_API_KEY doesn't leark to client. Something like
export const getGiphyInstance = createServerOnlyFn(() => {
const GIPHY_API_KEY = process.env.GIPHY_API_KEY

if(!GIPHY_API_KEY) throw new Error("GIPHY_API_KEY is not set");

return new GiphyFetch(GIPHY_API_KEY);
});

// Then call
export const fetchGifs = createServerFn({ method: "POST" })
.validator(data)
.handler(async ({ data }): Promise<GifsResult> => {
const { offset, term } = data;
const giphyFetch = getGiphyInstance();

if (term === "") {
console.log("fetchGifs called for trending with offset:", offset);
return giphyFetch.trending({ offset, limit: 10 });
}

return giphyFetch.search(term, { offset, limit: 10 });
});
export const getGiphyInstance = createServerOnlyFn(() => {
const GIPHY_API_KEY = process.env.GIPHY_API_KEY

if(!GIPHY_API_KEY) throw new Error("GIPHY_API_KEY is not set");

return new GiphyFetch(GIPHY_API_KEY);
});

// Then call
export const fetchGifs = createServerFn({ method: "POST" })
.validator(data)
.handler(async ({ data }): Promise<GifsResult> => {
const { offset, term } = data;
const giphyFetch = getGiphyInstance();

if (term === "") {
console.log("fetchGifs called for trending with offset:", offset);
return giphyFetch.trending({ offset, limit: 10 });
}

return giphyFetch.search(term, { offset, limit: 10 });
});
conscious-sapphire
conscious-sapphireOP2mo ago
Oh I see, I thought createServerFn is already server only?

Did you find this page helpful?