Skip to content

Commit

Permalink
refactor: improve URL handling in fetch function and display version …
Browse files Browse the repository at this point in the history
…in index page
  • Loading branch information
jqshuv committed Oct 20, 2024
1 parent e065007 commit cedcf0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

// import version from package.json
import { version } from '../../package.json';

export default {
async fetch(request, env): Promise<Response> {
// Get the short code from the request URL.
Expand All @@ -21,7 +24,7 @@ export default {
if (request.method !== 'GET') return new Response('Method Not Allowed', { status: 405 });

// Show index page if no short code is provided.
if (!shortCodeGet) return new Response('just redirect. - powered by jqshuv x unately.', { status: 200 });
if (!shortCodeGet) return new Response(`just redirect. (v${version}) - powered by jqshuv x unately.`, { status: 200 });

// Get the redirect URL from the short code.
const data = await env.SHORT_URLS.get(shortCodeGet);
Expand Down
3 changes: 2 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { env, createExecutionContext, waitOnExecutionContext } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
import worker from '../src/index';
import { version } from '../package.json';

const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;

Expand All @@ -19,7 +20,7 @@ describe('Hello World worker', () => {
const response = await worker.fetch(request, env);
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
await waitOnExecutionContext(ctx);
expect(await response.text()).toMatchInlineSnapshot("\"just redirect. - powered by jqshuv x unately.\"");
expect(await response.text()).toMatchInlineSnapshot(`"just redirect. (v${version}) - powered by jqshuv x unately."`);
});

it('post request url', async () => {
Expand Down

0 comments on commit cedcf0e

Please sign in to comment.