Add parkio

This commit is contained in:
2023-09-04 15:20:01 -06:00
commit 19eb9915fa
20 changed files with 5585 additions and 0 deletions

25
.eslintrc.js Normal file
View File

@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};

35
.gitignore vendored Normal file
View File

@@ -0,0 +1,35 @@
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

4
.prettierrc Normal file
View File

@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}

51
README.md Normal file
View File

@@ -0,0 +1,51 @@
# api.us.dev
## Description
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
## Installation
```bash
$ yarn install
```
## Running the app
```bash
# development
$ yarn run start
# watch mode
$ yarn run start:dev
# production mode
$ yarn run start:prod
```
## Test
```bash
# unit tests
$ yarn run test
# e2e tests
$ yarn run test:e2e
# test coverage
$ yarn run test:cov
```
## Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
## Stay in touch
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)
## License
Nest is [MIT licensed](LICENSE).

8
nest-cli.json Normal file
View File

@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}

73
package.json Normal file
View File

@@ -0,0 +1,73 @@
{
"name": "us-api",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"axios": "^1.5.0",
"ramda": "^0.29.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/axios": "^0.14.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/ramda": "^0.29.3",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}

View File

@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

12
src/app.controller.ts Normal file
View File

@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

11
src/app.module.ts Normal file
View File

@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ParkioModule } from './parkio/parkio.module';
@Module({
imports: [ParkioModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

8
src/app.service.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

8
src/main.ts Normal file
View File

@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

View File

@@ -0,0 +1,33 @@
import { Controller, Get, Param } from '@nestjs/common';
import { ParkioService } from './parkio.service';
@Controller('parkio')
export class ParkioController {
constructor(
private readonly parkioService: ParkioService
) {}
@Get('auctions')
async getAuctions () {
return this.parkioService.fetchAuctions();
}
@Get('domains')
async getDomains () {
return this.parkioService.fetchAllDomains();
}
@Get('short/:maxLength')
async getShort (@Param('maxLength') maxLength: number) {
return {
maxLength,
domains: (await this.parkioService.fetchAllDomains()).filter(
parsedDomain => parsedDomain.domain_length <= maxLength
),
auctions: (await this.parkioService.fetchAuctions()).filter(
parsedAuction => parsedAuction.domain_length <= maxLength
),
}
}
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ParkioService } from './parkio.service';
import { ParkioController } from './parkio.controller';
@Module({
providers: [ParkioService],
controllers: [ParkioController]
})
export class ParkioModule {}

View File

@@ -0,0 +1,117 @@
import { Injectable } from '@nestjs/common';
import {
Auction,
AuctionsResponse,
Domain,
DomainSet,
DomainsResponse,
ParkioTld,
ParsedAuction,
ParsedDomain,
} from './types';
import axios from 'axios';
import { filter, map, pipe } from 'ramda';
const parkIoEndpoints = {
domains: 'https://park.io/domains.json',
auctions: 'https://park.io/auctions.json',
tld: (tld: ParkioTld) => `https://park.io/domains/index/${tld}.json`,
all: (limit: number = 10_000) =>
`https://park.io/domains/index/all.json?limit=${limit}`,
};
@Injectable()
export class ParkioService {
fetchDomainsForTld = async (tld: ParkioTld) => {};
fetchAllDomains = async (limit: number = 10_000): Promise<ParsedDomain[]> => {
let response = await axios.get<DomainsResponse>(parkIoEndpoints.all(limit));
// There may have been more than our set limit
if (response.data.current === limit) {
return await this.fetchAllDomains(limit + 200);
}
return map(this.parseDomain, response.data.domains);
};
fetchAuctions = async (): Promise<ParsedAuction[]> => {
const response = await axios.get<AuctionsResponse>(
parkIoEndpoints.auctions,
);
return map(this.parseAuction, response.data.auctions);
};
lengthFilter = (length: number) => (parsedDomain: ParsedDomain) =>
parsedDomain.domain.length === length;
buildExtensions = (domains: ParsedDomain[], extensions: ParkioTld[]) =>
pipe(
map((extension: ParkioTld) =>
pipe(
filter(this.tldFilter(extension)),
filter(this.maxLengthFilter(3)),
)(domains),
),
)(extensions);
extractDomain = (domain: string): string =>
domain.split('.').slice(0, -1).join('.');
extractTld = (domain: string): string =>
domain.split('.').slice(-1).join('.');
parseDomain = (domain: Domain): ParsedDomain => {
const domainName = this.extractDomain(domain.name);
return {
...domain,
id: Number(domain.id),
domain: domainName,
domain_length: domainName.length,
date_available: domain.date_available
? this.parseDate(domain.date_available)
: undefined,
date_registered: domain.date_registered
? this.parseDate(domain.date_registered)
: undefined,
};
};
parseDate = (date: string) =>
new Date(
Date.parse(
date.replace(
/(\d+)-(\d+)-(\d+)UTC(\d+:\d+:\d{0,2})\d*/,
'$1-$3-$2T$4Z',
),
),
);
parseAuction = (auction: Auction): ParsedAuction => {
const domain = this.extractDomain(auction.name);
const tld = this.extractTld(auction.name);
return {
...auction,
id: Number(auction.id),
num_bids: Number(auction.num_bids),
price: Number(auction.price),
close_date: this.parseDate(auction.close_date),
created: this.parseDate(auction.created),
domain,
domain_length: domain.length,
tld,
};
};
domainToString = (domain: ParsedDomain) =>
`${domain.name}\t${domain.date_available}`;
auctionToString = (auction: ParsedAuction) =>
`${auction.name}\t$${auction.price}\t${auction.close_date}`;
tldFilter = (tld: ParkioTld) => (domain: ParsedDomain) => domain.tld === tld;
maxLengthFilter = (length: number) => (parsedDomain: ParsedDomain) =>
parsedDomain.domain.length <= length;
}

83
src/parkio/types.ts Normal file
View File

@@ -0,0 +1,83 @@
export const TLDs = [
'io',
'co',
'us',
'to',
'gg',
'ly',
'vc',
'me',
'sh',
'bz',
'ag',
'sc',
'ac',
'lc',
'je',
'mn',
'pro',
'info',
'red',
] as const;
export type ParkioTld = typeof TLDs[number];
export type DomainSet = { [key: string]: ParsedDomain[] };
export interface Domain {
id: string;
name: string;
date_available: string;
date_registered: string;
tld: string;
}
export interface Auction {
id: string;
name: string;
num_bids: string;
price: string;
close_date: string;
created: string;
}
export interface ParsedAuction {
id: number;
name: string;
domain: string;
domain_length: number;
tld: string;
num_bids: number;
price: number;
close_date: Date;
created: Date;
}
export interface ParsedDomain {
id: number;
domain: string;
domain_length: number
name: string;
tld: string;
date_available?: Date;
date_registered?: Date;
}
export interface BaseResponse {
page: number,
current: number,
count: number,
prevPage: boolean,
nextPage: boolean,
pageCount: number;
limit: number,
success: boolean,
}
export interface DomainsResponse extends BaseResponse{
domains: Domain[]
}
export interface AuctionsResponse extends BaseResponse {
auctions: Auction[]
}

24
test/app.e2e-spec.ts Normal file
View File

@@ -0,0 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
});

9
test/jest-e2e.json Normal file
View File

@@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}

4
tsconfig.build.json Normal file
View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

21
tsconfig.json Normal file
View File

@@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
}
}

5028
yarn.lock Normal file

File diff suppressed because it is too large Load Diff