Skip to content

Commit

Permalink
Version 0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad-szychowiak committed Oct 11, 2020
2 parents 0d10b3b + 68c69f8 commit 7b85bcf
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 94 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] – 2020-10-??
### Added
- Smelter API:
+ support for `getRepoByName` to git**lab** smelter
+ support for `getRepoByID` to git**hub** smelter



Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![npm type definitions](https://img.shields.io/npm/types/git-smelt?style=flat-square)
![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/git-smelt?style=flat-square)

> 🚧 A NPM package, that provides unified access to the APIs of different git hosting providers.
> 🚧 An NPM package, that unifies access to different git hosting services.
+ [Why?](#-why)
+ [Compatibility](#-compatibility) with JavaScript module models
Expand Down Expand Up @@ -97,7 +97,7 @@ Currently, you can access some of the **GitHub**'s and **GitLab**'s API endpoint

> 🚧 Detailed API docs are in progress.
>
> You can download https://github.com/konrad-szychowiak/git-smelt/settings
> You can download https://github.com/konrad-szychowiak/git-smelt
> install the deps and run `gulp docs` to get `docs/` folder with html docs from TypeDoc.
### Users
Expand All @@ -118,16 +118,22 @@ Fetch info about:
+ (`github`) a repository
+ (`gitlab`) a project

#### `getRepoByID()`
#### `getRepoByID(id: number | string)`
```ts
gitlab().getRepoByID(id)
// .then()
// .catch()
github().getRepoByID(id)
// .then()
// .catch()
```

#### `getRepoByName()`
#### `getRepoByName(owner: string, repo: string)`
Requires the name of the repository, including its owner.
```ts
gitlab().getRepoByName('sparks-team', 'po-drodze')
// .then()
// .catch()
github().getRepoByName('konrad-szychowiak', 'git-smelt')
// .then()
// .catch()
Expand All @@ -137,7 +143,7 @@ github().getRepoByName('konrad-szychowiak', 'git-smelt')
## ⏲️ [Changelog]

## ✍️ Contributors
+ **[konrad-szychowiak]** [@szychowiakk] main creator
+ **[konrad-szychowiak]** main creator
+ **[srflp]**

## ⚖️ License
Expand All @@ -147,5 +153,4 @@ github().getRepoByName('konrad-szychowiak', 'git-smelt')
[MPL-2.0]: ./LICENSE
[Changelog]: ./CHANGELOG.md
[konrad-szychowiak]: https://github.com/konrad-szychowiak
[@szychowiakk]: https://twitter.com/szychowiakk
[srflp]: https://github.com/srflp
16 changes: 1 addition & 15 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
const {
task, src, dest, series,
} = require('gulp');
const ts = require('gulp-typescript');
const { task } = require('gulp');
const del = require('del');

task('clean', () => del('lib/**', { force: true }));

task('build', () => src(['src/**/*.ts', '!src/**/*.test.ts', '!src/**/__mock__'])
.pipe(ts({
target: 'es5',
module: 'commonjs',
declaration: true,
experimentalDecorators: true,
}))
.pipe(dest('lib')));

exports.default = series('clean', 'build');
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "git-smelt",
"version": "0.2.2",
"description": "A NPM package, that provides unified access to the APIs of different git hosting providers.",
"version": "0.2.3",
"description": "An NPM package, that unifies access to different git hosting services.",
"keywords": [
"git",
"git providers",
Expand All @@ -13,7 +13,7 @@
"typescript",
"smelt"
],
"repository": "https://github.com/konrad-szychowiak/git-smelt/settings",
"repository": "https://github.com/konrad-szychowiak/git-smelt",
"license": "MPL-2.0",
"main": "lib/index.js",
"module": "lib/index.mjs",
Expand Down
23 changes: 4 additions & 19 deletions src/github/smelter/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Octokit } from '@octokit/core';
import { AxiosResponse } from 'axios';
import { BaseSmelter } from '../../baseSmelter';
import { WIPError } from '../../errors';

export class GitHubSmelter extends BaseSmelter {
constructor(auth?: string) {
Expand All @@ -15,11 +14,6 @@ export class GitHubSmelter extends BaseSmelter {
return this._client;
}

/**
* GET data about a given user
* from GitHub API.
* @param {string} username GitHub login.
*/
async getUser(username) {
const res = await this.client.request('GET /users/:username', {
username,
Expand All @@ -28,22 +22,13 @@ export class GitHubSmelter extends BaseSmelter {
return data;
}

/**
* GET data about a project
* from GitLab API
* based on the project's ID.
* @param {string} repoID id of the gitlab project you are looking for
*/
async getRepoByID(repoID: string) {
throw new WIPError(repoID, this);
// TODO: assure id is numeric
const res = await this.client.request('GET /repositories/:id', { id: repoID });
const { data } = res as unknown as AxiosResponse<object[]>;
return data;
}

/**
* GET data about a given repo
* from GitHub API.
* @param {string} owner login of the repo's owner.
* @param {string} repo name of the repo.
*/
async getRepoByName(owner: string, repo: string) {
const res = await this.client.request('GET /repos/{owner}/{repo}', {
owner,
Expand Down
9 changes: 8 additions & 1 deletion src/gitlab/smelter/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiURLFromInstanceName, GitLabSmelter } from './index';
import { apiURLFromInstanceName, encodeURIFromPath, GitLabSmelter } from './index';
import { GitLabEngine } from '../engine';
import { BaseSmelter } from '../../baseSmelter';

Expand All @@ -15,6 +15,13 @@ test("Properly generate API address from GitLab instance's domain name.", () =>
expect(API_ADDRESS).toBe(`https://${INSTANCE_ADDRESS}/api/v4`);
});

test("URI should be properly encoded to pass it as a gitlab project's id.", () => {
const pathElements = ['hello', 'world'];
const encodedURIFromBuiltInEncoder = encodeURIComponent('hello/world');
const encodedUIFromTheFunction = encodeURIFromPath(...pathElements);
expect(encodedUIFromTheFunction).toMatch(encodedURIFromBuiltInEncoder);
});

describe('Many ways to create a smelter', () => {
const fromConstructor = new GitLabSmelter(ENGINE);
const fromURL = GitLabSmelter.fromURL(API_ADDRESS);
Expand Down
45 changes: 11 additions & 34 deletions src/gitlab/smelter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function apiURLFromInstanceName(instanceName) {
return `https://${instanceName}/api/v4`;
}

export function encodeURIFromPath(...pathElements) {
const fullPath = pathElements.join('/');
return encodeURIComponent(fullPath);
}

export class GitLabSmelter extends BaseSmelter {
static fromURL(url: string): GitLabSmelter {
const engine = GitLabEngine.fromURL(url);
Expand All @@ -18,11 +23,6 @@ export class GitLabSmelter extends BaseSmelter {
return GitLabSmelter.fromURL(instanceURL);
}

/**
* Return an error, if given resource does not exist
* on the gitlab instance targeted by this smelter.
* @param {string} resourceDescription description of the requested resource.
*/
resourceError(resourceDescription: string) {
return ResourceNotOnInstanceError.fromEngine(this.engine as GitLabEngine, resourceDescription);
}
Expand All @@ -31,41 +31,18 @@ export class GitLabSmelter extends BaseSmelter {
return new WIPError(resourceDescription, this);
}

/**
* GET data about a given user
* from GitLab API.
* @param {string} username GitLab username.
*/
async getUser(username) {
const endpoint = `/users?username=${username}`;
const [data] = await this.engine.get(endpoint);
if (data !== {}) return data;
throw this.resourceError(`user with name: ${username}`);
return this.engine.get(endpoint);
}

/**
* GET data about a project
* from GitLab API
* based on the project's ID.
* @param {string} repoID id of the gitlab project you are looking for
*/
async getRepoByID(repoID: string) {
async getRepoByID(repoID: number | string) {
const endpoint = `/projects/${repoID}`;
const [data] = await this.engine.get(endpoint);
if (data) return data;
// TODO: Fix that
throw this.resourceError(`repo with id: ${repoID}`);
return this.engine.get(endpoint);
}

/**
* GET data about a project
* from GitLab API
* based on the project's ID.
* @param {string} repoID id of the gitlab project you are looking for
*/
// async
getRepoByName(/* owner: string, repo: string */) {
// TODO: find how to get repo by path from gitlab API
throw this.wipError('method: getRepoByName');
async getRepoByName(owner: string, repo: string) {
const repoURI = encodeURIFromPath(owner, repo);
return this.getRepoByID(repoURI);
}
}
14 changes: 14 additions & 0 deletions src/gitlab/tests/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { gitlab } from '../index';
import { ResourceNotOnInstanceError, WIPError } from '../../errors';

describe('GitLabSmelter should be able to throw an error when...', () => {
const smelter = gitlab();

test('resources are not found', () => {
expect(smelter.resourceError('')).toBeInstanceOf(ResourceNotOnInstanceError);
});

test('method is not implemented yet', () => {
expect(smelter.wipError('')).toBeInstanceOf(WIPError);
});
});
15 changes: 0 additions & 15 deletions src/gitlab/tests/getUser.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/tests/shortcutFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
gitlab, github, GitLabSmelter, GitHubSmelter,
github, GitHubSmelter, gitlab, GitLabSmelter,
} from '../index';

const ShortcutFunctionDescriptor = (shortcut: Function, smelter: Function) => ({
Expand Down

0 comments on commit 7b85bcf

Please sign in to comment.