From b07cb7e21d5477bbc4a9ea76965de73ba2f4a44e Mon Sep 17 00:00:00 2001 From: Adrian L Thomas Date: Thu, 21 Mar 2024 11:28:30 +0000 Subject: [PATCH] fix: lint --- README.md | 5 ++- package.json | 2 +- src/generate-token.spec.ts | 2 +- src/http/validation.spec.ts | 61 ++++++++++++++++++------------------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 805ab6c..ebe7996 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This project is an excuse to try out some new tools (namely Cloudflare Workers a `package.json` contains some commands for local development. `npm i && npm start` _should_ just work. As should `npm test`. If you wish to perform Cloudflare actions locally, then you'll need to run `npx wrangler login` first. Invoking the endpoint can be done like so: + ```bash curl -i -X POST http://localhost:8787/api/shorten?url=http://example.com # response returns an alternative url that can be used for requests and returns a 301 to the original URL ``` @@ -20,6 +21,7 @@ curl -i -X POST http://localhost:8787/api/shorten?url=http://example.com # respo Also done via GitHub Actions. However if you want to deploy manually, you can do so with Wrangler by running `npm run deploy`. # Database + ```bash npm run db:init # executes the migration script to setup the db npm run db:query # simple select statement against the DB @@ -34,6 +36,7 @@ npm run db:generate # generates a migration script if you change the schema ## Architecture The architecture is simple, it consists of: + - A CloudFlare Worker (serverless function, exposes a HTTP API) - Cloudflare D1 (SQLite serverless database: with automatic read replcias) @@ -48,8 +51,8 @@ Some decisions have been made in order to keep the project fun, but which would I may improve this at some point as it's own challenge, but the problem stands today. ### The "short url" isn't actually that short -The API design / routing could be reduced further. Also the domain name I've bound is my own, and isn't that short on it's own! But, it proves the concept. +The API design / routing could be reduced further. Also the domain name I've bound is my own, and isn't that short on it's own! But, it proves the concept. ## Positives diff --git a/package.json b/package.json index eb2dea5..917ef10 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "db:query": "npx wrangler d1 execute url-shortener --local --command='SELECT * FROM urls'", "db:init": "npx wrangler d1 execute url-shortener --local --file=./drizzle/0000_handy_bishop.sql", "db:generate": "drizzle-kit generate:sqlite --schema=src/db/schema.ts", - "test": "npm run test:unit && npm run test:integration", + "test": "npx jest --runInBand", "test:unit": "jest --runInBand **/*.spec.ts", "test:integration": "jest --runInBand test/**/*.test.ts", "lint:check": "prettier --check . ; eslint .", diff --git a/src/generate-token.spec.ts b/src/generate-token.spec.ts index 8741de5..98d1fa0 100644 --- a/src/generate-token.spec.ts +++ b/src/generate-token.spec.ts @@ -11,7 +11,7 @@ describe('shortenUrl', () => { ]; it('should always return a string of length 6', async () => { - const prefix = 'http://api.example.com' + const prefix = 'http://api.example.com'; for (const url of urls) { const result = await generateToken(url); expect(result).toHaveLength(6); diff --git a/src/http/validation.spec.ts b/src/http/validation.spec.ts index 894d48c..cd86348 100644 --- a/src/http/validation.spec.ts +++ b/src/http/validation.spec.ts @@ -1,38 +1,37 @@ import { urlValidation, tokenValidation } from './validation'; describe('validation', () => { - describe('urlValidation', () => { - it('should return true when url valid', async () => { - const result = await urlValidation('http://valid.url'); - expect(result).toBe(true); - }); - - it('should return false when url invalid', async () => { - const result = await urlValidation('invalid.url'); - expect(result).toBe(false); - }); - - it('should return false when url is null', async () => { - const result = await urlValidation(null); - expect(result).toBe(false); - }); + describe('urlValidation', () => { + it('should return true when url valid', async () => { + const result = await urlValidation('http://valid.url'); + expect(result).toBe(true); + }); - }) + it('should return false when url invalid', async () => { + const result = await urlValidation('invalid.url'); + expect(result).toBe(false); + }); - describe('tokenValidation', () => { - it('should return true when token valid', async () => { - const result = await tokenValidation('abcdef'); - expect(result).toBe(true); - }); - - it('should return false when token too short', async () => { - const result = await tokenValidation('a'); - expect(result).toBe(false); - }); + it('should return false when url is null', async () => { + const result = await urlValidation(null); + expect(result).toBe(false); + }); + }); - it('should return false when token too long', async () => { - const result = await tokenValidation('123456789'); - expect(result).toBe(false); - }); - }); + describe('tokenValidation', () => { + it('should return true when token valid', async () => { + const result = await tokenValidation('abcdef'); + expect(result).toBe(true); + }); + + it('should return false when token too short', async () => { + const result = await tokenValidation('a'); + expect(result).toBe(false); + }); + + it('should return false when token too long', async () => { + const result = await tokenValidation('123456789'); + expect(result).toBe(false); + }); + }); });