Skip to content

Commit

Permalink
refactor(all): use generics
Browse files Browse the repository at this point in the history
  • Loading branch information
galta95 committed Nov 29, 2020
1 parent b6cdffd commit 8718281
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 76 deletions.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -41,13 +42,15 @@
"@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",
"eslint": "^7.14.0",
"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"
}
}
51 changes: 10 additions & 41 deletions src/api/v6/index.ts
Original file line number Diff line number Diff line change
@@ -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<IResponse<number>> {
let res: AxiosResponse<number>;
try {
res = await axios(conf);
res = await this.httpClient.put<number>(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 };
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Apiv6 from './api/v6';

export default Apiv6;
export default Apiv6;
4 changes: 2 additions & 2 deletions src/lib/http-error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions src/lib/response-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export function response(code: number, message: string): {code: number, message: string} {
return {
code: code,
message: message
};
export interface IResponse<T = string> {
status: number,
data: T
}
2 changes: 1 addition & 1 deletion tests/unit/config/tests-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const testConf = {
url: 'http://test.com:8080',
baseUrl: 'http://test.com:8080',
username: 'USERNAME',
password: 'PASSWORD'
};
10 changes: 5 additions & 5 deletions src/lib/osm-xml.ts → tests/unit/lib/osm-xml.ts
Original file line number Diff line number Diff line change
@@ -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;
}
37 changes: 22 additions & 15 deletions tests/unit/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 8718281

Please sign in to comment.