From 8718281b2cb8fbfb2d870d58c6e294f83588cf75 Mon Sep 17 00:00:00 2001 From: galta <48960890+galta95@users.noreply.github.com> Date: Sun, 29 Nov 2020 13:25:33 +0200 Subject: [PATCH] refactor(all): use generics --- package.json | 15 +++++---- src/api/v6/index.ts | 51 ++++++------------------------ src/index.ts | 2 +- src/lib/http-error-handler.ts | 4 +-- src/lib/response-handler.ts | 8 ++--- tests/unit/config/tests-config.ts | 2 +- {src => tests/unit}/lib/osm-xml.ts | 10 +++--- tests/unit/unit.test.ts | 37 +++++++++++++--------- 8 files changed, 53 insertions(+), 76 deletions(-) rename {src => tests/unit}/lib/osm-xml.ts (63%) diff --git a/package.json b/package.json index 759cccc..aca3b8b 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,11 @@ }, "eslintConfig": { "extends": [ - "@map-colonies/eslint-config/ts-base" - ] + "@map-colonies/eslint-config/ts-base" + ], + "parserOptions": { + "project": "./tsconfig.json" + } }, "author": "galta95", "license": "ISC", @@ -29,9 +32,7 @@ "homepage": "https://github.com/MapColonies/node-osm-api#readme", "dependencies": { "@types/node": "^14.14.9", - "@types/xml": "^1.0.5", - "axios": "^0.21.0", - "xml": "^1.0.1" + "axios": "^0.21.0" }, "devDependencies": { "@commitlint/config-conventional": "^11.0.0", @@ -41,6 +42,7 @@ "@types/nock": "^11.1.0", "@typescript-eslint/eslint-plugin": "^4.8.1", "@typescript-eslint/parser": "^4.8.1", + "@types/xml": "^1.0.5", "typescript": "^4.1.2", "chai": "^4.2.0", "commitlint": "^11.0.0", @@ -48,6 +50,7 @@ "husky": "^4.3.0", "mocha": "^8.2.1", "nock": "^13.0.5", - "ts-node": "^9.0.0" + "ts-node": "^9.0.0", + "xml": "^1.0.1" } } diff --git a/src/api/v6/index.ts b/src/api/v6/index.ts index 71e5aa7..3a62ef8 100644 --- a/src/api/v6/index.ts +++ b/src/api/v6/index.ts @@ -1,55 +1,24 @@ -import axios from 'axios'; -import { AxiosRequestConfig } from 'axios'; -import { response } from '../../lib/response-handler'; +import axios, { AxiosInstance, AxiosResponse } from 'axios'; +import { IResponse } from '../../lib/response-handler'; import { createChangesetEndPoint } from '../../lib/endpoints'; -import { createChangesetXml } from '../../lib/osm-xml'; import HttpErrorHandler from '../../lib/http-error-handler'; class Apiv6 { - url: string; - username: string; - password: string; + private readonly httpClient: AxiosInstance; - constructor(url: string, username: string, password: string) { - this.url = url; - this.username = username; - this.password = password; - } - - getUrl(): string { - return this.url; - } - getUsername(): string { - return this.url; - } - getPassword(): string { - return this.password; - } - - setUrl(url: string): void { - this.url = url; - } - setCreds(username: string, password: string): void { - this.username = username; - this.password = password; + public constructor(private readonly baseUrl: string, username: string, password: string) { + this.httpClient = axios.create({ baseURL: baseUrl, auth: { username, password } }); } - public async createChangeset(generator: string, createdBy: string): Promise<{ code: number, message: string }> { - const data = createChangesetXml(generator, createdBy, this.url); - const conf: AxiosRequestConfig = { - url: this.url + createChangesetEndPoint, - method: 'put', - data: data, - auth: { username: this.username, password: this.password } - }; - let res; + public async createChangeset(data: string): Promise> { + let res: AxiosResponse; try { - res = await axios(conf); + res = await this.httpClient.put(createChangesetEndPoint, data); } catch (e) { throw new HttpErrorHandler(e); } - const { data: changeSetId, status: code } = res; - return response(code, changeSetId); + const { status, data: changeSetId } = res; + return { status, data: changeSetId }; } } diff --git a/src/index.ts b/src/index.ts index 8c4bf7f..1ccb111 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ import Apiv6 from './api/v6'; -export default Apiv6; +export default Apiv6; \ No newline at end of file diff --git a/src/lib/http-error-handler.ts b/src/lib/http-error-handler.ts index 8e154dc..09dc2cb 100644 --- a/src/lib/http-error-handler.ts +++ b/src/lib/http-error-handler.ts @@ -3,9 +3,9 @@ export interface MyError extends Error { } class HttpErrorHandler extends Error { - status: number + private readonly status: number - constructor(error: MyError) { + public constructor(error: MyError) { super(error.response.data); this.status = error.response.status; Object.setPrototypeOf(this, HttpErrorHandler.prototype); diff --git a/src/lib/response-handler.ts b/src/lib/response-handler.ts index 1b85022..8ef8028 100644 --- a/src/lib/response-handler.ts +++ b/src/lib/response-handler.ts @@ -1,6 +1,4 @@ -export function response(code: number, message: string): {code: number, message: string} { - return { - code: code, - message: message - }; +export interface IResponse { + status: number, + data: T } \ No newline at end of file diff --git a/tests/unit/config/tests-config.ts b/tests/unit/config/tests-config.ts index 930900e..a93952e 100644 --- a/tests/unit/config/tests-config.ts +++ b/tests/unit/config/tests-config.ts @@ -1,5 +1,5 @@ export const testConf = { - url: 'http://test.com:8080', + baseUrl: 'http://test.com:8080', username: 'USERNAME', password: 'PASSWORD' }; \ No newline at end of file diff --git a/src/lib/osm-xml.ts b/tests/unit/lib/osm-xml.ts similarity index 63% rename from src/lib/osm-xml.ts rename to tests/unit/lib/osm-xml.ts index f461958..ffc7e3f 100644 --- a/src/lib/osm-xml.ts +++ b/tests/unit/lib/osm-xml.ts @@ -1,18 +1,18 @@ +/* eslint-disable @typescript-eslint/naming-convention */ import xml from 'xml'; -export function createChangesetXml(generator: string, createdBy: string, host: string): string { +export function createChangesetXml(generator: string, createdBy: string): string { const data = [{ osm: [ { changeset: [ - {_attr: {version: 0.6, generator: generator}}, - { tag: {_attr: {k: "created_by", v: createdBy}}}, - { tag: {_attr: {k: "host", v: host}} }, + { _atter: {version: 0.6, generator: generator}}, + { tag: {_attr: {k: "created_by", v: createdBy}}} ]}] }]; return createtXml(data); } -function createtXml(file: any) { +function createtXml(file: any): string { const osmXml = xml(file); return osmXml; } \ No newline at end of file diff --git a/tests/unit/unit.test.ts b/tests/unit/unit.test.ts index 915d8da..72ae84d 100644 --- a/tests/unit/unit.test.ts +++ b/tests/unit/unit.test.ts @@ -2,34 +2,41 @@ import { expect } from 'chai'; import nock = require('nock'); import Apiv6 from '../../src/index'; -import { testConf } from './config/tests-config'; import { createChangesetEndPoint } from '../../src/lib/endpoints'; +import { testConf } from './config/tests-config'; +import { createChangesetXml } from './lib/osm-xml'; -const { url, username, password } = testConf; +const { baseUrl, username, password } = testConf; -describe('apiv6', async function () { - const apiv6 = new Apiv6(url, username, password); - describe('happy flow', async function () { - describe('/changeset/create', async function () { - describe('with register user', async function () { +describe('apiv6', function () { + describe('happy flow', function () { + describe('/changeset/create', function () { + describe('with register user', function () { it('should return 200 and changset number', async function () { - nock(url).put(createChangesetEndPoint).reply(200, '12'); - const res = await apiv6.createChangeset("test-generator", "test-user"); + const apiv6 = new Apiv6(baseUrl, username, password); + + nock(baseUrl).put(createChangesetEndPoint).reply(200, '12'); + + const xmlData = createChangesetXml("test-generator", "test-user"); + const res = await apiv6.createChangeset(xmlData); expect(res).to.be.a('object') - .with.property('code') + .with.property('status') .and.to.be.equal(200); - expect(res).to.have.property('message') + expect(res).to.have.property('data') .and.to.be.equal(12); }); }); - describe('with unregisterd user', async function () { + describe('with unregisterd user', function () { it('should return error', async function () { - nock(url).put(createChangesetEndPoint).reply(401, "Couldn't authenticate you"); - apiv6.setCreds('not-registerd', '123456'); + const apiv6 = new Apiv6(baseUrl, 'not-registerd', '123456'); + + nock(baseUrl).put(createChangesetEndPoint).reply(401, "Couldn't authenticate you"); + + const xmlData = createChangesetXml("test-generator", "test-user"); try { - await apiv6.createChangeset("test-generator", "test-user"); + await apiv6.createChangeset(xmlData); } catch (e) { expect(e).to.be.a('Error') .with.property('message')