@@ -8,48 +8,69 @@ import {
88 previewTokenUpdate ,
99} from "./api-clients/client" ;
1010
11- interface BasePreviewTokenInfo {
11+ interface BaseHostTokenInfo {
1212 expiresAt : Date | null ;
1313 tokenId : string ;
1414 lastUsedAt : Date | null ;
1515}
1616
17- export interface PreviewTokenInfo extends BasePreviewTokenInfo {
17+ export interface HostTokenInfo extends BaseHostTokenInfo {
1818 tokenPrefix : string ;
1919}
2020
21- export interface PreviewToken extends BasePreviewTokenInfo {
21+ export interface HostToken extends BaseHostTokenInfo {
2222 token : string ;
2323 sandboxId : string ;
2424}
2525
2626/**
27- * Provider for generating preview tokens that can be used to access
28- * private sandbox previews . This provider is only available in environments
27+ * Provider for generating host tokens that can be used to access
28+ * private sandbox hosts . This provider is only available in environments
2929 * with an authenticated API client (like Node.js).
3030 */
31- export class PreviewTokens extends Disposable {
31+ export class HostTokens extends Disposable {
3232 constructor ( private apiClient : Client ) {
3333 super ( ) ;
3434 }
3535
3636 /**
37- * Get a signed preview URL for a port using a preview token.
37+ * Get url to access a private host using a host token.
38+ * The PORT argument is needed as all hosts are exposed with
39+ * a port.
3840 */
39- getSignedPreviewUrl (
41+ getUrl (
4042 token : { sandboxId : string ; token : string } ,
41- port : number
43+ port : number ,
44+ protocol : string = "https"
4245 ) : string {
43- return `https ://${ token . sandboxId } -${ port } .csb.app?preview_token=${ token . token } ` ;
46+ return `${ protocol } ://${ token . sandboxId } -${ port } .csb.app?preview_token=${ token . token } ` ;
4447 }
4548
4649 /**
47- * Generate a new preview token that can be used to access private sandbox previews. By default the token never expires .
50+ * Get headers to access a private host using a host token.
4851 */
49- async create (
52+ getHeaders ( token : { sandboxId : string ; token : string } ) {
53+ return {
54+ "csb-preview-token" : token . token ,
55+ } ;
56+ }
57+
58+ /**
59+ * Get cookies to access a private host using a host token.
60+ */
61+ getCookies ( token : { sandboxId : string ; token : string } ) {
62+ return {
63+ csb_preview_token : token . token ,
64+ } ;
65+ }
66+
67+ /**
68+ * Generate a new host token that can be used to access private sandbox hosts. By default the token never expires.
69+ */
70+ async createToken (
5071 sandboxId : string ,
5172 opts : { expiresAt ?: Date } = { }
52- ) : Promise < PreviewToken > {
73+ ) : Promise < HostToken > {
5374 const response = handleResponse (
5475 await previewTokenCreate ( {
5576 client : this . apiClient ,
@@ -81,17 +102,17 @@ export class PreviewTokens extends Disposable {
81102 }
82103
83104 /**
84- * List all active preview tokens for this sandbox.
105+ * List all active host tokens for this sandbox.
85106 */
86- async list ( sandboxId : string ) : Promise < PreviewTokenInfo [ ] > {
107+ async listTokens ( sandboxId : string ) : Promise < HostTokenInfo [ ] > {
87108 const response = handleResponse (
88109 await previewTokenList ( {
89110 client : this . apiClient ,
90111 path : {
91112 id : sandboxId ,
92113 } ,
93114 } ) ,
94- "Failed to list preview tokens"
115+ "Failed to list host tokens"
95116 ) ;
96117
97118 if ( ! response . tokens ) {
@@ -107,9 +128,9 @@ export class PreviewTokens extends Disposable {
107128 }
108129
109130 /**
110- * Revoke a single preview token for this sandbox.
131+ * Revoke a single host token for this sandbox.
111132 */
112- async revoke ( sandboxId : string , tokenId : string ) : Promise < void > {
133+ async revokeToken ( sandboxId : string , tokenId : string ) : Promise < void > {
113134 handleResponse (
114135 await previewTokenUpdate ( {
115136 client : this . apiClient ,
@@ -121,35 +142,35 @@ export class PreviewTokens extends Disposable {
121142 expires_at : new Date ( ) . toISOString ( ) ,
122143 } ,
123144 } ) ,
124- "Failed to revoke preview token"
145+ "Failed to revoke host token"
125146 ) ;
126147 }
127148
128149 /**
129- * Revoke all active preview tokens for this sandbox.
150+ * Revoke all active host tokens for this sandbox.
130151 * This will immediately invalidate all tokens, and they can no longer be used
131- * to access the sandbox preview .
152+ * to access the sandbox host .
132153 */
133- async revokeAll ( sandboxId : string ) : Promise < void > {
154+ async revokeAllTokens ( sandboxId : string ) : Promise < void > {
134155 handleResponse (
135156 await previewTokenRevokeAll ( {
136157 client : this . apiClient ,
137158 path : {
138159 id : sandboxId ,
139160 } ,
140161 } ) ,
141- "Failed to revoke preview tokens"
162+ "Failed to revoke host tokens"
142163 ) ;
143164 }
144165
145166 /**
146- * Update a preview token's expiration date.
167+ * Update a host token's expiration date.
147168 */
148- async update (
169+ async updateToken (
149170 sandboxId : string ,
150171 tokenId : string ,
151172 expiresAt : Date | null
152- ) : Promise < PreviewTokenInfo > {
173+ ) : Promise < HostTokenInfo > {
153174 const response = handleResponse (
154175 await previewTokenUpdate ( {
155176 client : this . apiClient ,
@@ -161,7 +182,7 @@ export class PreviewTokens extends Disposable {
161182 expires_at : expiresAt ?. toISOString ( ) ,
162183 } ,
163184 } ) ,
164- "Failed to update preview token"
185+ "Failed to update host token"
165186 ) ;
166187
167188 if ( ! response . token ) {
0 commit comments