-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9261c72
feat(apiv06): new route /api/0.6/changeset/#id/close
galta95 fe11da1
style(prettier): format all documents
galta95 7dd2274
refactor(apiv06): export api res messages to constants
galta95 16194fa
style(prettier): added print width to 150
galta95 10a8f73
style(rename-file): error-handler to errors
galta95 85424fe
style(constants): rename constants
galta95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ jobs: | |
run: npm ci | ||
|
||
- name: Run tests | ||
run: npm run test | ||
run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"printWidth": 150, | ||
"semi": true, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# node-osm-api | ||
|
||
easy communication with osm api | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = { extends: ['@commitlint/config-conventional'] }; | ||
module.exports = { extends: ['@commitlint/config-conventional'] }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const OWNER_MISMATCH = "The user doesn't own that changeset"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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