Skip to content

Commit

Permalink
feat: add proxy option
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 6, 2019
1 parent 01c803d commit 65a0ee1
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packages/@vssue/api-bitbucket-v2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class BitbucketV2 implements VssueAPI.Instance {
clientId: string
clientSecret: string
state: string
proxy: string | ((url: string) => string)
$http: AxiosInstance

constructor ({
Expand All @@ -41,6 +42,7 @@ export default class BitbucketV2 implements VssueAPI.Instance {
clientId,
clientSecret,
state,
proxy,
}: VssueAPI.Options) {
this.baseURL = baseURL
this.owner = owner
Expand All @@ -49,6 +51,7 @@ export default class BitbucketV2 implements VssueAPI.Instance {
this.clientId = clientId
this.clientSecret = clientSecret
this.state = state
this.proxy = proxy

this.$http = axios.create({
baseURL: 'https://api.bitbucket.org/2.0',
Expand Down Expand Up @@ -123,7 +126,11 @@ export default class BitbucketV2 implements VssueAPI.Instance {
}: {
code: string
}): Promise<string> {
const { data } = await this.$http.post(`https://cors-anywhere.herokuapp.com/${concatURL(this.baseURL, 'site/oauth2/access_token')}`, buildQuery({
const originalURL = concatURL(this.baseURL, 'site/oauth2/access_token')
const proxyURL = typeof this.proxy === 'function'
? this.proxy(originalURL)
: this.proxy
const { data } = await this.$http.post(proxyURL, buildQuery({
grant_type: 'authorization_code',
redirect_uri: window.location.href,
code,
Expand Down
9 changes: 8 additions & 1 deletion packages/@vssue/api-github-v3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class GithubV3 implements VssueAPI.Instance {
clientId: string
clientSecret: string
state: string
proxy: string | ((url: string) => string)
$http: AxiosInstance

constructor ({
Expand All @@ -44,6 +45,7 @@ export default class GithubV3 implements VssueAPI.Instance {
clientId,
clientSecret,
state,
proxy,
}: VssueAPI.Options) {
this.baseURL = baseURL
this.owner = owner
Expand All @@ -53,6 +55,7 @@ export default class GithubV3 implements VssueAPI.Instance {
this.clientId = clientId
this.clientSecret = clientSecret
this.state = state
this.proxy = proxy

this.$http = axios.create({
baseURL: baseURL === 'https://github.com' ? 'https://api.github.com' : concatURL(baseURL, 'api/v3'),
Expand Down Expand Up @@ -143,7 +146,11 @@ export default class GithubV3 implements VssueAPI.Instance {
* access_token api does not support cors
* @see https://github.com/isaacs/github/issues/330
*/
const { data } = await this.$http.post(`https://cors-anywhere.herokuapp.com/${concatURL(this.baseURL, 'login/oauth/access_token')}`, {
const originalURL = concatURL(this.baseURL, 'login/oauth/access_token')
const proxyURL = typeof this.proxy === 'function'
? this.proxy(originalURL)
: this.proxy
const { data } = await this.$http.post(proxyURL, {
client_id: this.clientId,
client_secret: this.clientSecret,
code,
Expand Down
9 changes: 8 additions & 1 deletion packages/@vssue/api-github-v4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class GithubV4 implements VssueAPI.Instance {
clientId: string
clientSecret: string
state: string
proxy: string | ((url: string) => string)
$http: AxiosInstance

private _pageInfo: {
Expand All @@ -53,6 +54,7 @@ export default class GithubV4 implements VssueAPI.Instance {
clientId,
clientSecret,
state,
proxy,
}: VssueAPI.Options) {
this.baseURL = baseURL
this.owner = owner
Expand All @@ -62,6 +64,7 @@ export default class GithubV4 implements VssueAPI.Instance {
this.clientId = clientId
this.clientSecret = clientSecret
this.state = state
this.proxy = proxy

this._pageInfo = {
page: 1,
Expand Down Expand Up @@ -163,7 +166,11 @@ export default class GithubV4 implements VssueAPI.Instance {
* access_token api does not support cors
* @see https://github.com/isaacs/github/issues/330
*/
const { data } = await this.$http.post(`https://cors-anywhere.herokuapp.com/${concatURL(this.baseURL, 'login/oauth/access_token')}`, {
const originalURL = concatURL(this.baseURL, 'login/oauth/access_token')
const proxyURL = typeof this.proxy === 'function'
? this.proxy(originalURL)
: this.proxy
const { data } = await this.$http.post(proxyURL, {
client_id: this.clientId,
client_secret: this.clientSecret,
code,
Expand Down
9 changes: 8 additions & 1 deletion packages/@vssue/api-gitlab-v4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class GitlabV4 implements VssueAPI.Instance {
clientId: string
clientSecret: string
state: string
proxy: string | ((url: string) => string)
$http: AxiosInstance

private _encodedRepo: string
Expand All @@ -46,6 +47,7 @@ export default class GitlabV4 implements VssueAPI.Instance {
clientId,
clientSecret,
state,
proxy,
}: VssueAPI.Options) {
this.baseURL = baseURL
this.owner = owner
Expand All @@ -55,6 +57,7 @@ export default class GitlabV4 implements VssueAPI.Instance {
this.clientId = clientId
this.clientSecret = clientSecret
this.state = state
this.proxy = proxy

// @see https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
this._encodedRepo = encodeURIComponent(`${this.owner}/${this.repo}`)
Expand Down Expand Up @@ -137,7 +140,11 @@ export default class GitlabV4 implements VssueAPI.Instance {
}: {
code: string
}): Promise<string> {
const { data } = await this.$http.post(`https://cors-anywhere.herokuapp.com/${concatURL(this.baseURL, 'oauth/token')}`, {
const originalURL = concatURL(this.baseURL, 'oauth/token')
const proxyURL = typeof this.proxy === 'function'
? this.proxy(originalURL)
: this.proxy
const { data } = await this.$http.post(proxyURL, {
client_id: this.clientId,
client_secret: this.clientSecret,
code,
Expand Down
7 changes: 4 additions & 3 deletions packages/vssue/dev/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import VssuePlguin, {
VssueComponent,
} from 'vssue'
// @ts-ignore
import GithubV3 from '@vssue/api'
import PlatformAPI from '@vssue/api'

import 'vssue/dist/vssue.css'
import 'github-markdown-css'

const onlyComponent: boolean = process.env.VUE_APP_ONLY_COMPONENT === 'true'

const options: Vssue.Options = {
api: GithubV3,
const options: Partial<Vssue.Options> = {
api: PlatformAPI,
owner: process.env.VUE_APP_OWNER,
repo: process.env.VUE_APP_REPO,
clientId: process.env.VUE_APP_CLIENT_ID,
Expand All @@ -22,6 +22,7 @@ const options: Vssue.Options = {
prefix: '[Vssue]',
admins: [],
perPage: 5,
proxy: url => `https://cors-anywhere.herokuapp.com/${url}`,
}

if (!onlyComponent) {
Expand Down
2 changes: 2 additions & 0 deletions packages/vssue/src/VssueStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class VssueStore extends Vue implements Vssue.Store {
prefix: '[Vssue]',
admins: [],
perPage: 10,
proxy: (url: string): string => `https://cors-anywhere.herokuapp.com/${url}`,
}, options)

// check options
Expand Down Expand Up @@ -152,6 +153,7 @@ class VssueStore extends Vue implements Vssue.Store {
repo: this.options.repo,
clientId: this.options.clientId,
clientSecret: this.options.clientSecret,
proxy: this.options.proxy,
})

// handle authorization
Expand Down
1 change: 1 addition & 0 deletions packages/vssue/types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export namespace VssueAPI {
baseURL?: string
state: string
labels: Array<string>
proxy: string | ((url: string) => string)
}

export type Platform = {
Expand Down
7 changes: 4 additions & 3 deletions packages/vssue/types/vssue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ export namespace Vssue {
repo: string
clientId: string
clientSecret: string
baseURL?: string
baseURL: string
state: string
labels: Array<string>
prefix: string
admins: Array<string>
perPage: number
locale?: string
locale: string
proxy: string | ((url: string) => string)
}

export interface Plugin extends PluginObject<Vssue.Options> {
export interface Plugin extends PluginObject<Partial<Vssue.Options>> {
readonly version: string
installed: boolean
VssueComponent: Vssue.Component
Expand Down

0 comments on commit 65a0ee1

Please sign in to comment.