diff --git a/package-lock.json b/package-lock.json index d8bb14c2..75db940c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3525,6 +3525,22 @@ } } }, + "eslint-plugin-prettier": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-2.7.0.tgz", + "integrity": "sha512-CStQYJgALoQBw3FsBzH0VOVDRnJ/ZimUlpLm226U8qgqYJfPOY/CPK6wyRInMxh73HSKg5wyRwdS4BVYYHwokA==", + "requires": { + "fast-diff": "^1.1.1", + "jest-docblock": "^21.0.0" + }, + "dependencies": { + "jest-docblock": { + "version": "21.2.0", + "resolved": "https://registry.npmjs.org/jest-docblock/-/jest-docblock-21.2.0.tgz", + "integrity": "sha512-5IZ7sY9dBAYSV+YjQ0Ovb540Ku7AO9Z5o2Cg789xj167iQuZ2cG+z0f3Uct6WeYLbU6aQiM2pCs7sZ+4dotydw==" + } + } + }, "esprima": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", @@ -3779,6 +3795,11 @@ "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=", "dev": true }, + "fast-diff": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz", + "integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==" + }, "fast-glob": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.0.4.tgz", @@ -8380,8 +8401,7 @@ "lines-and-columns": { "version": "1.1.6", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.1.6.tgz", - "integrity": "sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=", - "dev": true + "integrity": "sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=" }, "lint-staged": { "version": "9.2.3", @@ -15207,8 +15227,7 @@ "tslib": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz", - "integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==", - "dev": true + "integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==" }, "tslint": { "version": "5.19.0", @@ -15315,6 +15334,16 @@ } } }, + "tslint-plugin-prettier": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/tslint-plugin-prettier/-/tslint-plugin-prettier-2.0.1.tgz", + "integrity": "sha512-4FX9JIx/1rKHIPJNfMb+ooX1gPk5Vg3vNi7+dyFYpLO+O57F4g+b/fo1+W/G0SUOkBLHB/YKScxjX/P+7ZT/Tw==", + "requires": { + "eslint-plugin-prettier": "^2.2.0", + "lines-and-columns": "^1.1.6", + "tslib": "^1.7.1" + } + }, "tsutils": { "version": "2.29.0", "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz", diff --git a/package.json b/package.json index ae86dea5..9483e200 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "ky-universal": "^0.3.0", "li": "^1.3.0", "query-string": "^6.8.2", + "tslint-plugin-prettier": "^2.0.1", "universal-url": "^2.0.0" }, "devDependencies": { @@ -80,9 +81,9 @@ "build": "tsc && rollup -c", "commit": "npx git-cz", "coverage": "codecov", - "lint": "tslint -p . -t stylish", - "lint:doc:fix": "prettier --write './*.json' './*.yml' './*.md'", - "lint:fix": "npm run lint -- --fix", + "lint": "tslint -p . -t stylish && prettier --check './**/*.ts'", + "lint:doc:fix": "prettier --write './*.json' './*.yml' './*.md' './*.js'", + "lint:fix": "prettier --write './**/*.ts' && npm run lint -- --fix", "prepublishOnly": "npm run build", "release": "semantic-release", "test:integration": "jest test/integration -c=jest.config.json --detectOpenHandles", diff --git a/rollup.config.js b/rollup.config.js index 97e2e6d7..d74cedb9 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,7 +2,7 @@ import ts from 'rollup-plugin-typescript2'; import resolve from 'rollup-plugin-node-resolve'; import commonjs from 'rollup-plugin-commonjs'; import json from 'rollup-plugin-json'; -import { terser } from "rollup-plugin-terser"; +import { terser } from 'rollup-plugin-terser'; import builtins from 'rollup-plugin-node-builtins'; import globals from 'rollup-plugin-node-globals'; import typescript from 'typescript'; @@ -18,14 +18,14 @@ export default [ format: 'umd', exports: 'named', globals: { - 'li': 'Li', - 'humps': 'Humps', + li: 'Li', + humps: 'Humps', 'query-string': 'QueryString', - 'randomstring': 'RandomString', + randomstring: 'RandomString', 'ky-universal': 'Ky', 'form-data': 'FormData', - 'universal-url':'universal-url' - } + 'universal-url': 'universal-url', + }, }, external: [...Object.keys(pkg.dependencies)], plugins: [ @@ -47,12 +47,7 @@ export default [ format: 'cjs', }, external: [...Object.keys(pkg.dependencies)], - plugins: [ - globals(), - builtins(), - ts({ typescript }), - terser(), - ], + plugins: [globals(), builtins(), ts({ typescript }), terser()], }, // ES module (for bundlers) build. @@ -63,9 +58,6 @@ export default [ format: 'es', }, external: [...Object.keys(pkg.dependencies)], - plugins: [ - ts({ typescript }), - terser(), - ], + plugins: [ts({ typescript }), terser()], }, ]; diff --git a/src/infrastructure/KyRequester.ts b/src/infrastructure/KyRequester.ts index 93ab50c5..d49c9e42 100644 --- a/src/infrastructure/KyRequester.ts +++ b/src/infrastructure/KyRequester.ts @@ -69,7 +69,7 @@ methods.forEach(m => { e.description = output.error || output.message; } - + throw e; } diff --git a/src/infrastructure/RequestHelper.ts b/src/infrastructure/RequestHelper.ts index 5e7c9e04..f33e59d0 100644 --- a/src/infrastructure/RequestHelper.ts +++ b/src/infrastructure/RequestHelper.ts @@ -41,13 +41,13 @@ export async function get( // Rescurse through pagination results if (!query.page && underLimit && pagination.next) { const { next } = Li.parse(headers.link); - const leaf = service.url.split('/').pop() || "" - const regex = new RegExp(`.+\/api\/v\\d(\/${leaf})?\/`) - const more = await get(service, next.replace(regex, ''), { + const leaf = service.url.split('/').pop() || ''; + const regex = new RegExp(`.+\/api\/v\\d(\/${leaf})?\/`); + const more = (await get(service, next.replace(regex, ''), { maxPages, sudo, - showPagination: true - }) as PaginationResponse; + showPagination: true, + })) as PaginationResponse; pagination = more.pagination; body = [...body, ...more.data]; @@ -56,11 +56,7 @@ export async function get( return (query.page || body.length > 0) && showPagination ? { data: body, pagination } : body; } -export function stream( - service: BaseService, - endpoint: string, - options: BaseRequestOptions = {}, -) { +export function stream(service: BaseService, endpoint: string, options: BaseRequestOptions = {}) { if (typeof service.requester.stream !== 'function') { throw new Error('Stream method is not implementated in requester!'); } diff --git a/src/services/Commits.ts b/src/services/Commits.ts index 3da55114..1fa3e3f5 100644 --- a/src/services/Commits.ts +++ b/src/services/Commits.ts @@ -88,7 +88,11 @@ class Commits extends BaseService { mergeRequests(projectId: ProjectId, sha: string, options?: BaseRequestOptions) { const pId = encodeURIComponent(projectId); - return RequestHelper.get(this, `projects/${pId}/repository/commits/${sha}/merge_requests`, options); + return RequestHelper.get( + this, + `projects/${pId}/repository/commits/${sha}/merge_requests`, + options, + ); } } diff --git a/src/services/Groups.ts b/src/services/Groups.ts index 3c3a762d..d726eb44 100644 --- a/src/services/Groups.ts +++ b/src/services/Groups.ts @@ -44,7 +44,6 @@ class Groups extends BaseService { cn, { provider, ...options }: Sudo & { provider?: string } = {}, ) { - const gId = encodeURIComponent(groupId); const url = provider ? `${provider}/${cn}` : `${cn}`; diff --git a/src/services/Labels.ts b/src/services/Labels.ts index 7324c9d8..fefc47f1 100644 --- a/src/services/Labels.ts +++ b/src/services/Labels.ts @@ -17,7 +17,11 @@ class Labels extends BaseService { create(projectId: ProjectId, labelName: string, color: string, options?: BaseRequestOptions) { const pId = encodeURIComponent(projectId); - return RequestHelper.post(this, `projects/${pId}/labels`, { name: labelName, color, ...options }); + return RequestHelper.post(this, `projects/${pId}/labels`, { + name: labelName, + color, + ...options, + }); } edit(projectId: ProjectId, labelName: string, options?: BaseRequestOptions) { diff --git a/src/services/Projects.ts b/src/services/Projects.ts index 35303bf8..24a9ee85 100644 --- a/src/services/Projects.ts +++ b/src/services/Projects.ts @@ -37,7 +37,10 @@ class Projects extends BaseService { return RequestHelper.get(this, `projects/${pId}/events`, options); } - fork(projectId: ProjectId, { forkedFromId, ...options }: { forkedFromId?: number } & BaseRequestOptions = {}) { + fork( + projectId: ProjectId, + { forkedFromId, ...options }: { forkedFromId?: number } & BaseRequestOptions = {}, + ) { const pId = encodeURIComponent(projectId); let url = `projects/${pId}/fork`; @@ -133,7 +136,7 @@ class Projects extends BaseService { return RequestHelper.put(this, `projects/${pId}/push_rule`, options); } - upload(projectId, content, { metadata, sudo }: { metadata?: UploadMetadata } & Sudo = {}) { + upload(projectId, content, { metadata, sudo }: { metadata?: UploadMetadata } & Sudo = {}) { const pId = encodeURIComponent(projectId); const form = new FormData(); diff --git a/src/templates/ResourceDiscussions.ts b/src/templates/ResourceDiscussions.ts index 26d3b7f1..2ee6accb 100644 --- a/src/templates/ResourceDiscussions.ts +++ b/src/templates/ResourceDiscussions.ts @@ -11,11 +11,7 @@ import { ResourceId, DiscussionId, NoteId } from '..'; class ResourceDiscussions extends BaseService { protected resource2Type: string; - constructor( - resourceType: string, - resource2Type: string, - options: BaseServiceOptions, - ) { + constructor(resourceType: string, resource2Type: string, options: BaseServiceOptions) { super({ url: resourceType, ...options }); this.resource2Type = resource2Type; diff --git a/src/templates/ResourceNotes.ts b/src/templates/ResourceNotes.ts index 6b3d28ce..b0c7d2e0 100644 --- a/src/templates/ResourceNotes.ts +++ b/src/templates/ResourceNotes.ts @@ -11,11 +11,7 @@ import { ResourceId, NoteId } from '..'; class ResourceNotes extends BaseService { protected resource2Type: string; - constructor( - resourceType: string, - resource2Type: string, - options: BaseServiceOptions, - ) { + constructor(resourceType: string, resource2Type: string, options: BaseServiceOptions) { super({ url: resourceType, ...options }); this.resource2Type = resource2Type; diff --git a/test/integration/services/ApplicationSettings.ts b/test/integration/services/ApplicationSettings.ts index 36ebf8e5..7e8be498 100644 --- a/test/integration/services/ApplicationSettings.ts +++ b/test/integration/services/ApplicationSettings.ts @@ -11,7 +11,7 @@ beforeAll(async () => { describe('ApplicationSettings.all', () => { let settings; - + beforeEach(async () => { settings = await service.all(); }); diff --git a/test/integration/services/Issues.ts b/test/integration/services/Issues.ts index 9033937d..d30557ff 100644 --- a/test/integration/services/Issues.ts +++ b/test/integration/services/Issues.ts @@ -53,7 +53,7 @@ describe('Issues.all', () => { }); it('should return a list filtered to a specfic page', async () => { - const issues1 = await service.all({projectId: project.id, perPage: 1, page: 1 }); + const issues1 = await service.all({ projectId: project.id, perPage: 1, page: 1 }); expect(issues1).toBeInstanceOf(Array); expect(issues1).toHaveLength(1); diff --git a/test/integration/services/Labels.ts b/test/integration/services/Labels.ts index 0463b415..be8ad43a 100644 --- a/test/integration/services/Labels.ts +++ b/test/integration/services/Labels.ts @@ -31,7 +31,7 @@ describe('Labels.remove', () => { it('should remove/delete a valid label on a project', async () => { const label = await service.create(project.id, 'Test Label3', '#FFAABB'); - expect(service.remove(project.id, label.name)).resolves.toEqual(""); + expect(service.remove(project.id, label.name)).resolves.toEqual(''); }); }); @@ -55,14 +55,18 @@ describe('Labels.all', () => { }); it('should return a list of labels on a project restricted to page 5', async () => { - const labels = await service.all(project.id, { perPage: 5, page: 5}); + const labels = await service.all(project.id, { perPage: 5, page: 5 }); expect(labels).toBeInstanceOf(Array); expect(labels).toHaveLength(5); }); it('should return a list of labels on a project restricted to page 5 and show the pagination object', async () => { - const { data, pagination } = await service.all(project.id, { perPage: 5, page: 5, showPagination: true}); + const { data, pagination } = await service.all(project.id, { + perPage: 5, + page: 5, + showPagination: true, + }); expect(data).toBeInstanceOf(Array); expect(data).toHaveLength(5); @@ -75,4 +79,4 @@ describe('Labels.all', () => { totalPages: 11, }); }); -}); \ No newline at end of file +}); diff --git a/test/integration/services/Projects.ts b/test/integration/services/Projects.ts index 3ab3cc0b..f880da29 100644 --- a/test/integration/services/Projects.ts +++ b/test/integration/services/Projects.ts @@ -3,36 +3,36 @@ import { Projects } from '../../../dist'; let service: Projects; beforeEach(() => { - service = new Projects({ - host: process.env.GITLAB_URL, - token: process.env.PERSONAL_ACCESS_TOKEN, - }); + service = new Projects({ + host: process.env.GITLAB_URL, + token: process.env.PERSONAL_ACCESS_TOKEN, + }); }); describe('Projects.create', () => { - it('should create a valid project', async () => { - const p = await service.create({ name: 'Project Creation Integration test' }); - expect(p).toBeInstanceOf(Object); - expect(p.name).toEqual('Project Creation Integration test'); - }); + it('should create a valid project', async () => { + const p = await service.create({ name: 'Project Creation Integration test' }); + expect(p).toBeInstanceOf(Object); + expect(p.name).toEqual('Project Creation Integration test'); + }); }); describe('Projects.upload', () => { - let project: object; + let project: object; - beforeAll(async () => { - project = await service.create({ name: 'Project Upload Integration test' }); - }); + beforeAll(async () => { + project = await service.create({ name: 'Project Upload Integration test' }); + }); - it('should upload a text file', async () => { - const content = 'TESTING FILE UPLOAD :D'; - const results = await service.upload(project.id, content, { - metadata: { - filename: 'testfile.txt', - contentType: 'text/plain', - }, - }); + it('should upload a text file', async () => { + const content = 'TESTING FILE UPLOAD :D'; + const results = await service.upload(project.id, content, { + metadata: { + filename: 'testfile.txt', + contentType: 'text/plain', + }, + }); - expect(results).toContainKeys(['alt', 'url', 'markdown']); - }); + expect(results).toContainKeys(['alt', 'url', 'markdown']); + }); }); diff --git a/test/unit/infrastructure/BaseService.ts b/test/unit/infrastructure/BaseService.ts index ee9cee7f..665f30dd 100644 --- a/test/unit/infrastructure/BaseService.ts +++ b/test/unit/infrastructure/BaseService.ts @@ -39,41 +39,41 @@ describe('Creation of BaseService instance', () => { }); test('Camelize option should return simple response with camelized keys', async () => { - const service = new BaseService({ host: 'https://testing.com', token: '1234', camelize: true}); + const service = new BaseService({ host: 'https://testing.com', token: '1234', camelize: true }); service.show = jest.fn(() => RequestHelper.get(service, 'test')); KyRequester.get = jest.fn(() => ({ body: { id: 3, gravatar_enable: true }, headers: {} })); const results = await service.show(); - expect(results).toMatchObject({ id: 3, gravatarEnable: true }) + expect(results).toMatchObject({ id: 3, gravatarEnable: true }); }); test('Camelize option should return simple response with camelized keys', async () => { - const service = new BaseService({ host: 'https://testing.com', token: '1234', camelize: true}); + const service = new BaseService({ host: 'https://testing.com', token: '1234', camelize: true }); service.show = jest.fn(() => RequestHelper.get(service, 'test')); KyRequester.get = jest.fn(() => ({ - body: [ - { id: 3, gravatar_enable: true }, - { id: 4, gravatar_enable: false }, - ], - headers: {} + body: [{ id: 3, gravatar_enable: true }, { id: 4, gravatar_enable: false }], + headers: {}, })); const results = await service.show(); - expect(results).toIncludeSameMembers([{ id: 3, gravatarEnable: true }, { id: 4, gravatarEnable: false }]) + expect(results).toIncludeSameMembers([ + { id: 3, gravatarEnable: true }, + { id: 4, gravatarEnable: false }, + ]); }); test('Camelize option unset should return simple response with default keys', async () => { - const service = new BaseService({ host: 'https://testing.com', token: '1234'}); + const service = new BaseService({ host: 'https://testing.com', token: '1234' }); service.show = jest.fn(() => RequestHelper.get(service, 'test')); KyRequester.get = jest.fn(() => ({ body: { id: 3, gravatar_enable: true }, headers: {} })); const results = await service.show(); - expect(results).toMatchObject({ id: 3, gravatar_enable: true }) + expect(results).toMatchObject({ id: 3, gravatar_enable: true }); }); }); diff --git a/test/unit/infrastructure/RequestHelper.ts b/test/unit/infrastructure/RequestHelper.ts index a3da61fe..5add4c9a 100644 --- a/test/unit/infrastructure/RequestHelper.ts +++ b/test/unit/infrastructure/RequestHelper.ts @@ -11,7 +11,7 @@ const mockedGetBasic = () => ({ }, }); -const mockedGetExtended = (url, { query }, limit=30) => { +const mockedGetExtended = (url, { query }, limit = 30) => { const pages: object[] = []; const q = url.match(/page=([0-9]+)/); let page = 1; @@ -21,20 +21,20 @@ const mockedGetExtended = (url, { query }, limit=30) => { // Only load pages needed for the test // TODO: modify this to only generate the required response, without the loop - for (let i = 1, a = 1, b = 2; i <= limit && i <= page; i++, a+=2, b+=2) { + for (let i = 1, a = 1, b = 2; i <= limit && i <= page; i++, a += 2, b += 2) { let next = ''; let prev = ''; let nextPage; let prevPage; - if ((i+1) <= limit ) { - next = `; rel="next",`; - nextPage = i+1 + if (i + 1 <= limit) { + next = `; rel="next",`; + nextPage = i + 1; } - if ((i-1) >= 1) { - prev = `; rel="prev",`; - prevPage = i-1 + if (i - 1 >= 1) { + prev = `; rel="prev",`; + prevPage = i - 1; } pages.push({ @@ -49,7 +49,10 @@ const mockedGetExtended = (url, { query }, limit=30) => { }, ], headers: { - link: next + prev + ` + link: + next + + prev + + ` ; rel="first", ; rel="last"`, 'x-next-page': nextPage, @@ -63,7 +66,7 @@ const mockedGetExtended = (url, { query }, limit=30) => { } return pages[page - 1]; -} +}; const service = new BaseService({ host: 'https://testing.com', diff --git a/tslint.json b/tslint.json index e4501976..b0441738 100644 --- a/tslint.json +++ b/tslint.json @@ -1,5 +1,5 @@ { - "extends": ["tslint-config-airbnb", "tslint-config-prettier"], + "extends": ["tslint-config-airbnb", "tslint-plugin-prettier", "tslint-config-prettier"], "rules": { "variable-name": [true, "ban-keywords", "check-format", "allow-pascal-case"], "object-shorthand-properties-first": false,