Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianLThomas committed Mar 21, 2024
1 parent 72795f2 commit b07cb7e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
2 changes: 1 addition & 1 deletion src/generate-token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
61 changes: 30 additions & 31 deletions src/http/validation.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit b07cb7e

Please sign in to comment.