Skip to content

Commit

Permalink
add: env check for api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
matfire committed Jan 20, 2024
1 parent d660bfa commit e92bf7c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/routes/api/giphy/+server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { GIPHY_TOKEN } from '$env/static/private';
import { env } from '$env/dynamic/private';
import { GiphyFetch } from '@giphy/js-fetch-api';
import { json, type RequestHandler } from '@sveltejs/kit';

const client = new GiphyFetch(GIPHY_TOKEN);
import { fail, json, type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async ({ url }) => {
const { GIPHY_TOKEN } = env;
if (!GIPHY_TOKEN) {
throw fail(500, { message: 'could not find giphy configuration' });
}
const client = new GiphyFetch(GIPHY_TOKEN);
const query = url.searchParams.get('query');
const pageParam = url.searchParams.get('page');
const page = pageParam ? parseInt(pageParam) : 0;
Expand Down
8 changes: 6 additions & 2 deletions src/routes/api/unsplash/+server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { UNSPLASH_TOKEN } from '$env/static/private';
import { json, type RequestHandler } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { fail, json, type RequestHandler } from '@sveltejs/kit';
import { createApi } from 'unsplash-js';
const client = createApi({ accessKey: UNSPLASH_TOKEN });

export const GET: RequestHandler = async ({ url }) => {
const { UNSPLASH_TOKEN } = env;
if (!UNSPLASH_TOKEN) {
throw fail(500, { message: 'could not find unsplash configuration' });
}
const query = url.searchParams.get('query');
const page = url.searchParams.has('page') ? parseInt(url.searchParams.get('page')) : 1;
if (query) {
Expand Down

0 comments on commit e92bf7c

Please sign in to comment.