Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apiv6): implemented closeChangeset #12

Merged
merged 6 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ jobs:
github_token: ${{ secrets.github_token }}
# Enable linters
eslint: true
eslint_extensions: ts
prettier: true
eslint_extensions: ts
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
run: npm ci

- name: Run tests
run: npm run test
run: npm run test
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"printWidth": 150,
"semi": true,
"singleQuote": true
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# node-osm-api

easy communication with osm api
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add documentation to README.md how to close a changeset, add a code snippet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do it at the end

2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
module.exports = { extends: ['@commitlint/config-conventional'] };
24 changes: 8 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,22 @@
"http-status-codes": "^2.1.4"
},
"devDependencies": {
"@types/node": "^14.14.9",
"@commitlint/config-conventional": "^11.0.0",
"@map-colonies/eslint-config": "^1.1.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.0.4",
"@types/nock": "^11.1.0",
"@types/node": "^14.14.9",
"@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",
"xml": "^1.0.1"
"typescript": "^4.1.2",
"prettier": "^2.2.1"
}
}
83 changes: 57 additions & 26 deletions src/api/v6/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import StatusCodes from 'http-status-codes';
import { createChangesetEndPoint } from '../../lib/endpoints';
import { UnauthorizedError, BadXmlError, InternalServerError } from '../../lib/error-handler';
import { createChangesetEndPoint, closeChangesetEndPoint } from '../../lib/endpoints';
import {
UnauthorizedError,
BadXmlError,
ChangesetNotFoundError,
OwnerMismatchError,
NotAllowedError,
ChangesetAlreadyClosedError,
} from '../../lib/errors';
import { OWNER_MISMATCH } from '../../lib/constants';
class Apiv6 {
private readonly httpClient: AxiosInstance;

public constructor(private readonly baseUrl: string, username: string, password: string) {
this.httpClient = axios.create({ baseURL: baseUrl, auth: { username, password } });
private readonly httpClient: AxiosInstance;

public constructor(private readonly baseUrl: string, username: string, password: string) {
this.httpClient = axios.create({
baseURL: baseUrl,
auth: { username, password },
});
}

public async createChangeset(data: string): Promise<number> {
let res: AxiosResponse<number>;
try {
res = await this.httpClient.put<number>(createChangesetEndPoint, data);
} catch (e) {
const axiosError = e as AxiosError;

if (axiosError.response?.status === StatusCodes.BAD_REQUEST) {
throw new BadXmlError(axiosError);
} else if (axiosError.response?.status === StatusCodes.UNAUTHORIZED) {
throw new UnauthorizedError(axiosError);
} else {
throw new Error(e);
}
}

public async createChangeset(data: string): Promise<number> {
let res: AxiosResponse<number>;
try {
res = await this.httpClient.put<number>(createChangesetEndPoint, data);
}
catch (e) {
const axiosError = e as AxiosError;
const { data: changeSetId } = res;
return changeSetId;
}

public async closeChangeset(id: number): Promise<void> {
try {
await this.httpClient.put<number>(closeChangesetEndPoint(id));
} catch (e) {
const axiosError = e as AxiosError;

if (axiosError.response?.status === StatusCodes.BAD_REQUEST) {
throw new BadXmlError(axiosError);
} else if (axiosError.response?.status === StatusCodes.UNAUTHORIZED) {
throw new UnauthorizedError(axiosError);
} else if (axiosError.response?.status === StatusCodes.INTERNAL_SERVER_ERROR) {
throw new InternalServerError(axiosError);
} else {
throw new Error(e);
}
if (axiosError.response?.status === StatusCodes.UNAUTHORIZED) {
throw new UnauthorizedError(axiosError);
} else if (axiosError.response?.status === StatusCodes.METHOD_NOT_ALLOWED) {
throw new NotAllowedError(axiosError);
} else if (axiosError.response?.status === StatusCodes.NOT_FOUND) {
throw new ChangesetNotFoundError(axiosError);
} else if (axiosError.response?.status === StatusCodes.CONFLICT) {
if (axiosError.response.data === OWNER_MISMATCH) {
throw new OwnerMismatchError(axiosError);
}
const { data: changeSetId } = res;
return changeSetId;
throw new ChangesetAlreadyClosedError(axiosError);
} else {
throw new Error(e);
}
}
}
}

export default Apiv6;
export default Apiv6;
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;
1 change: 1 addition & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const OWNER_MISMATCH = "The user doesn't own that changeset";
3 changes: 2 additions & 1 deletion src/lib/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const createChangesetEndPoint = '/api/0.6/changeset/create';
export const createChangesetEndPoint = '/api/0.6/changeset/create';
export const closeChangesetEndPoint = (id: number): string => `/api/0.6/changeset/${id}/close`;
29 changes: 0 additions & 29 deletions src/lib/error-handler.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { AxiosError } from 'axios';

class HttpErrorHandler extends Error {
public constructor(error: AxiosError) {
super(error.response?.data);
Object.setPrototypeOf(this, HttpErrorHandler.prototype);
}
}

export class UnauthorizedError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, UnauthorizedError.prototype);
}
}

export class BadXmlError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, BadXmlError.prototype);
}
}

export class ChangesetNotFoundError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, ChangesetNotFoundError.prototype);
}
}

export class ChangesetAlreadyClosedError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, ChangesetAlreadyClosedError.prototype);
}
}

export class OwnerMismatchError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, OwnerMismatchError.prototype);
}
}

export class NotAllowedError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, NotAllowedError.prototype);
}
}
Loading