File tree 9 files changed +77
-30
lines changed
9 files changed +77
-30
lines changed Original file line number Diff line number Diff line change @@ -18,19 +18,19 @@ pnpm i @coderabbitai/bitbucket
18
18
### Cloud
19
19
20
20
``` ts
21
- import { createBitbucketCloudClient } from " @coderabbitai/bitbucket"
21
+ import { createBitbucketCloudClient , toBase64 } from " @coderabbitai/bitbucket"
22
22
import {
23
23
BITBUCKET_CLOUD_APP_PASSWORD ,
24
24
BITBUCKET_CLOUD_URL ,
25
25
BITBUCKET_CLOUD_USERNAME ,
26
26
} from " ./env.js"
27
27
28
- const basic = Buffer . from (
28
+ const basic = toBase64 (
29
29
BITBUCKET_CLOUD_USERNAME + " :" + BITBUCKET_CLOUD_APP_PASSWORD ,
30
- ). toString ( " base64 " )
30
+ )
31
31
32
- export const cloud = createBitbucketCloudClient ({
33
- baseUrl: BITBUCKET_CLOUD_URL ,
32
+ export const client = createBitbucketCloudClient ({
33
+ baseUrl: BITBUCKET_CLOUD_URL . toString () ,
34
34
headers: { Accept: " application/json" , Authorization: ` Basic ${basic } ` },
35
35
})
36
36
```
Original file line number Diff line number Diff line change
1
+ import { test } from "vitest"
2
+ import { fromBase64 , toBase64 } from "./base64.js"
3
+
4
+ test ( "toBase64" , ( { expect } ) => {
5
+ const based = toBase64 ( "Copyright © 2024 CodeRabbit" )
6
+ expect ( based ) . toBe ( "Q29weXJpZ2h0IMKpIDIwMjQgQ29kZVJhYmJpdA==" )
7
+ } )
8
+
9
+ test ( "fromBase64" , ( { expect } ) => {
10
+ const debased = fromBase64 ( "Q29weXJpZ2h0IMKpIDIwMjQgQ29kZVJhYmJpdA==" )
11
+ expect ( debased ) . toBe ( "Copyright © 2024 CodeRabbit" )
12
+ } )
Original file line number Diff line number Diff line change
1
+ export function toBase64 ( input : string ) : string {
2
+ const bytes = new TextEncoder ( ) . encode ( input )
3
+ const string = String . fromCodePoint ( ...bytes )
4
+ return btoa ( string )
5
+ }
6
+
7
+ export function fromBase64 ( base64 : string ) : Buffer | Uint8Array | string {
8
+ const string = atob ( base64 )
9
+ const bytes = Uint8Array . from ( string , v => v . codePointAt ( 0 ) ?? 0 )
10
+ return new TextDecoder ( ) . decode ( bytes )
11
+ }
Original file line number Diff line number Diff line change 1
- import createClient , { type Client , type ClientOptions } from "openapi-fetch"
1
+ import type { Client , ClientOptions } from "openapi-fetch"
2
+ import createClient from "openapi-fetch"
2
3
import type { paths } from "./openapi/index.js"
3
4
5
+ /**
6
+ * Creates an `openapi-fetch` client using {@link createClient}.
7
+ *
8
+ * @example
9
+ * export client = createBitbucketCloudClient({
10
+ * baseUrl: "https://api.bitbucket.org/2.0",
11
+ * headers: { Accept: "application/json", Authorization: `Basic ${basic}` },
12
+ * })
13
+ */
4
14
export const createBitbucketCloudClient : (
5
15
clientOptions ?: ClientOptions ,
6
16
) => Client < paths , "application/json" > = createClient < paths , "application/json" >
Original file line number Diff line number Diff line change
1
+ export * from "./base64.js"
1
2
export * from "./cloud/index.js"
2
3
export * from "./server/index.js"
Original file line number Diff line number Diff line change @@ -2,6 +2,18 @@ import type { Client, ClientOptions } from "openapi-fetch"
2
2
import createClient from "openapi-fetch"
3
3
import type { paths } from "./openapi/index.js"
4
4
5
+ /**
6
+ * Creates an `openapi-fetch` client using {@link createClient}.
7
+ *
8
+ * @example
9
+ * export const client = createBitbucketServerClient({
10
+ * baseUrl: "https://example.org/rest",
11
+ * headers: {
12
+ * Accept: "application/json",
13
+ * Authorization: `Bearer ${BITBUCKET_SERVER_TOKEN}`,
14
+ * },
15
+ * })
16
+ */
5
17
export const createBitbucketServerClient : (
6
18
clientOptions ?: ClientOptions ,
7
19
) => Client < paths , "application/json" > = createClient < paths , "application/json" >
Original file line number Diff line number Diff line change 1
- import type { Client } from "openapi-fetch"
2
- import type { paths } from "../../src/cloud/openapi/openapi-typescript.js"
3
- import { createBitbucketCloudClient } from "../../src/index.js"
1
+ import { createBitbucketCloudClient , toBase64 } from "../../src/index.js"
4
2
import {
5
3
BITBUCKET_CLOUD_APP_PASSWORD ,
6
4
BITBUCKET_CLOUD_URL ,
7
5
BITBUCKET_CLOUD_USERNAME ,
8
6
} from "../env.js"
9
7
10
- const basic = Buffer . from (
8
+ const basic = toBase64 (
11
9
BITBUCKET_CLOUD_USERNAME + ":" + BITBUCKET_CLOUD_APP_PASSWORD ,
12
- ) . toString ( "base64" )
10
+ )
13
11
14
- export const cloud : Client < paths , "application/json" > =
15
- createBitbucketCloudClient ( {
16
- baseUrl : BITBUCKET_CLOUD_URL ,
17
- headers : { Accept : "application/json" , Authorization : `Basic ${ basic } ` } ,
18
- } )
12
+ export const cloud = createBitbucketCloudClient ( {
13
+ baseUrl : BITBUCKET_CLOUD_URL . toString ( ) ,
14
+ headers : { Accept : "application/json" , Authorization : `Basic ${ basic } ` } ,
15
+ } )
Original file line number Diff line number Diff line change @@ -18,6 +18,15 @@ function envString(key: string) {
18
18
return value
19
19
}
20
20
21
+ function envUrl ( key : string ) {
22
+ const str = envString ( key )
23
+ try {
24
+ return new URL ( str )
25
+ } catch ( error ) {
26
+ throw new Error ( `$${ key } is not a URL: ${ str } ` , { cause : error } )
27
+ }
28
+ }
29
+
21
30
export function isNodeEnv ( value : unknown ) : value is NodeEnv {
22
31
return Object . values < unknown > ( nodeEnvs ) . includes ( value )
23
32
}
@@ -72,12 +81,10 @@ const nodeEnvs = {
72
81
test : "test" ,
73
82
} as const
74
83
const parsed = loadEnv ( )
75
-
76
- export const BITBUCKET_CLOUD_URL = envString ( "BITBUCKET_CLOUD_URL" )
84
+ export const BITBUCKET_CLOUD_URL = envUrl ( "BITBUCKET_CLOUD_URL" )
77
85
export const BITBUCKET_CLOUD_USERNAME = envString ( "BITBUCKET_CLOUD_USERNAME" )
78
86
export const BITBUCKET_CLOUD_APP_PASSWORD = envString (
79
87
"BITBUCKET_CLOUD_APP_PASSWORD" ,
80
88
)
81
-
82
- export const BITBUCKET_SERVER_URL = envString ( "BITBUCKET_SERVER_URL" )
89
+ export const BITBUCKET_SERVER_URL = envUrl ( "BITBUCKET_SERVER_URL" )
83
90
export const BITBUCKET_SERVER_TOKEN = envString ( "BITBUCKET_SERVER_TOKEN" )
Original file line number Diff line number Diff line change 1
- import type { Client } from "openapi-fetch"
2
1
import { createBitbucketServerClient } from "../../src/index.js"
3
- import type { paths } from "../../src/server/openapi/openapi-typescript.js"
4
2
import { BITBUCKET_SERVER_TOKEN , BITBUCKET_SERVER_URL } from "../env.js"
5
3
6
- export const server : Client < paths , "application/json" > =
7
- createBitbucketServerClient ( {
8
- baseUrl : BITBUCKET_SERVER_URL ,
9
- headers : {
10
- Accept : "application/json" ,
11
- Authorization : `Bearer ${ BITBUCKET_SERVER_TOKEN } ` ,
12
- } ,
13
- } )
4
+ export const server = createBitbucketServerClient ( {
5
+ baseUrl : BITBUCKET_SERVER_URL . toString ( ) ,
6
+ headers : {
7
+ Accept : "application/json" ,
8
+ Authorization : `Bearer ${ BITBUCKET_SERVER_TOKEN } ` ,
9
+ } ,
10
+ } )
You can’t perform that action at this time.
0 commit comments