@shipstatic/ship 0.1.26 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as _shipstatic_types from '@shipstatic/types';
2
- import { Deployment, DeploymentListResponse, Alias, AliasListResponse, Account } from '@shipstatic/types';
3
- export { PingResponse, ShipError, ShipErrorType } from '@shipstatic/types';
2
+ import { PingResponse, ConfigResponse, StaticFile, Deployment, DeploymentListResponse, Alias, AliasListResponse, Account, DeployInput, DeploymentResource, AliasResource, AccountResource, KeysResource, PlatformConfig } from '@shipstatic/types';
3
+ export * from '@shipstatic/types';
4
+ export { Account, Alias, DEFAULT_API, DeployInput, Deployment, PingResponse, ShipError, ShipErrorType } from '@shipstatic/types';
4
5
 
5
6
  /**
6
7
  * @file SDK-specific type definitions
@@ -8,10 +9,6 @@ export { PingResponse, ShipError, ShipErrorType } from '@shipstatic/types';
8
9
  * Core types come from @shipstatic/types, while SDK-specific types are defined here.
9
10
  */
10
11
 
11
- /**
12
- * Consolidated input type for all environments
13
- */
14
- type DeployInput = FileList | File[] | HTMLInputElement | string[];
15
12
  /**
16
13
  * Universal deploy options for both Node.js and Browser environments
17
14
  */
@@ -87,92 +84,282 @@ interface ShipClientOptions {
87
84
  * Used if an deploy operation doesn't specify its own `maxConcurrency`.
88
85
  * Defaults to 4 if not set here or in the specific deploy call.
89
86
  */
90
- maxConcurrentDeploys?: number | undefined;
87
+ maxConcurrency?: number | undefined;
91
88
  /**
92
89
  * Default timeout in milliseconds for API requests made by this client instance.
93
90
  * Used if an deploy operation doesn't specify its own timeout.
94
91
  */
95
92
  timeout?: number | undefined;
96
93
  }
94
+
97
95
  /**
98
- * Represents a file that has been processed and is ready for deploy.
99
- * Used internally by the SDK and in advanced/manual deploy scenarios.
96
+ * Handles direct HTTP communication with the Ship API, including deploys and health checks.
97
+ * Responsible for constructing requests, managing authentication, and error translation.
98
+ * @internal
100
99
  */
101
- interface StaticFile {
100
+ declare class ApiHttp {
101
+ #private;
102
+ private readonly apiUrl;
103
+ private readonly apiKey;
104
+ private readonly deployToken;
102
105
  /**
103
- * The content of the file.
104
- * In Node.js, this is typically a `Buffer`.
105
- * In the browser, this is typically a `File` or `Blob` object.
106
+ * Constructs a new ApiHttp instance with the provided client options.
107
+ * @param options - Client options including API host, authentication credentials, and timeout settings.
106
108
  */
107
- content: File | Buffer | Blob;
109
+ constructor(options: ShipClientOptions);
108
110
  /**
109
- * The desired path for the file on the server, relative to the deployment root.
110
- * Should include the filename, e.g., `images/photo.jpg`.
111
+ * Sends a ping request to the Ship API server to verify connectivity and authentication.
112
+ * @returns Promise resolving to `true` if the ping is successful, `false` otherwise.
113
+ * @throws {ShipApiError} If the API returns an error response (4xx, 5xx).
114
+ * @throws {ShipNetworkError} If a network error occurs (e.g., DNS failure, connection refused).
111
115
  */
112
- path: string;
116
+ ping(): Promise<boolean>;
117
+ /**
118
+ * Get full ping response from the API server
119
+ * @returns Promise resolving to the full PingResponse object.
120
+ */
121
+ getPingResponse(): Promise<PingResponse>;
122
+ /**
123
+ * Fetches platform configuration from the API.
124
+ * @returns Promise resolving to the config response.
125
+ * @throws {ShipError} If the config request fails.
126
+ */
127
+ getConfig(): Promise<ConfigResponse>;
128
+ /**
129
+ * Deploys an array of StaticFile objects to the Ship API.
130
+ * Constructs and sends a multipart/form-data POST request, handling both browser and Node.js environments.
131
+ * Validates files and manages deploy progress and error translation.
132
+ * @param files - Array of StaticFile objects to deploy (must include MD5 checksums).
133
+ * @param options - Optional per-deploy configuration (overrides instance defaults).
134
+ * @returns Promise resolving to a full Deployment object on success.
135
+ * @throws {ShipFileError} If a file is missing an MD5 checksum or content type is unsupported.
136
+ * @throws {ShipClientError} If no files are provided or if environment is unknown.
137
+ * @throws {ShipNetworkError} If a network error occurs during deploy.
138
+ * @throws {ShipApiError} If the API returns an error response.
139
+ * @throws {ShipCancelledError} If the deploy is cancelled via an AbortSignal.
140
+ */
141
+ deploy(files: StaticFile[], options?: ApiDeployOptions): Promise<Deployment>;
142
+ /**
143
+ * Lists all deployments for the authenticated account
144
+ * @returns Promise resolving to deployment list response
145
+ */
146
+ listDeployments(): Promise<DeploymentListResponse>;
147
+ /**
148
+ * Gets a specific deployment by ID
149
+ * @param id - Deployment ID to retrieve
150
+ * @returns Promise resolving to deployment details
151
+ */
152
+ getDeployment(id: string): Promise<Deployment>;
153
+ /**
154
+ * Removes a deployment by ID
155
+ * @param id - Deployment ID to remove
156
+ * @returns Promise resolving when removal is complete
157
+ */
158
+ removeDeployment(id: string): Promise<void>;
159
+ /**
160
+ * Sets an alias (create or update)
161
+ * @param name - Alias name
162
+ * @param deployment - Deployment name to point to
163
+ * @returns Promise resolving to the created/updated alias with operation context
164
+ */
165
+ setAlias(name: string, deployment: string): Promise<_shipstatic_types.Alias>;
166
+ /**
167
+ * Gets a specific alias by name
168
+ * @param name - Alias name to retrieve
169
+ * @returns Promise resolving to alias details
170
+ */
171
+ getAlias(name: string): Promise<Alias>;
172
+ /**
173
+ * Lists all aliases for the authenticated account
174
+ * @returns Promise resolving to alias list response
175
+ */
176
+ listAliases(): Promise<AliasListResponse>;
177
+ /**
178
+ * Removes an alias by name
179
+ * @param name - Alias name to remove
180
+ * @returns Promise resolving to removal confirmation
181
+ */
182
+ removeAlias(name: string): Promise<void>;
183
+ /**
184
+ * Triggers a manual DNS check for an external alias
185
+ * @param name - Alias name to check DNS for
186
+ * @returns Promise resolving to confirmation message
187
+ */
188
+ checkAlias(name: string): Promise<{
189
+ message: string;
190
+ }>;
191
+ /**
192
+ * Gets account details for the authenticated user
193
+ * @returns Promise resolving to account details
194
+ */
195
+ getAccount(): Promise<Account>;
113
196
  /**
114
- * The original absolute file system path (primarily used in Node.js environments).
115
- * This helps in debugging or associating the server path back to its source.
197
+ * Creates a new API key for the authenticated user
198
+ * @returns Promise resolving to the new API key
116
199
  */
117
- filePath?: string;
200
+ createApiKey(): Promise<{
201
+ apiKey: string;
202
+ }>;
118
203
  /**
119
- * The MD5 hash (checksum) of the file's content.
120
- * This is calculated by the SDK before deploy if not provided.
204
+ * Checks if files represent a SPA structure using AI analysis
205
+ * @param files - Array of StaticFile objects to analyze
206
+ * @returns Promise resolving to boolean indicating if it's a SPA
121
207
  */
122
- md5?: string;
123
- /** The size of the file in bytes. */
124
- size: number;
208
+ checkSPA(files: StaticFile[]): Promise<boolean>;
125
209
  }
126
210
 
127
211
  /**
128
212
  * @file All Ship SDK resources in one place - impossibly simple.
129
213
  */
130
214
 
131
- interface DeploymentResource {
132
- create: (input: DeployInput, options?: DeploymentOptions) => Promise<Deployment>;
133
- list: () => Promise<DeploymentListResponse>;
134
- remove: (id: string) => Promise<void>;
135
- get: (id: string) => Promise<Deployment>;
136
- }
137
- interface AliasResource {
138
- set: (aliasName: string, deployment: string) => Promise<Alias>;
139
- get: (aliasName: string) => Promise<Alias>;
140
- list: () => Promise<AliasListResponse>;
141
- remove: (aliasName: string) => Promise<void>;
142
- check: (aliasName: string) => Promise<{
143
- message: string;
144
- }>;
145
- }
146
- interface AccountResource {
147
- get: () => Promise<Account>;
215
+ declare function createDeploymentResource(getApi: () => ApiHttp, clientDefaults?: ShipClientOptions, ensureInit?: () => Promise<void>, processInput?: (input: DeployInput, options: DeploymentOptions) => Promise<StaticFile[]>): DeploymentResource;
216
+ declare function createAliasResource(getApi: () => ApiHttp, ensureInit?: () => Promise<void>): AliasResource;
217
+ declare function createAccountResource(getApi: () => ApiHttp, ensureInit?: () => Promise<void>): AccountResource;
218
+ declare function createKeysResource(getApi: () => ApiHttp, ensureInit?: () => Promise<void>): KeysResource;
219
+
220
+ /**
221
+ * Abstract base class for Ship SDK implementations.
222
+ *
223
+ * Provides shared functionality while allowing environment-specific
224
+ * implementations to handle configuration loading and deployment processing.
225
+ */
226
+ declare abstract class Ship$1 {
227
+ protected http: ApiHttp;
228
+ protected readonly clientOptions: ShipClientOptions;
229
+ protected initPromise: Promise<void> | null;
230
+ protected _deployments: DeploymentResource;
231
+ protected _aliases: AliasResource;
232
+ protected _account: AccountResource;
233
+ protected _keys: KeysResource;
234
+ constructor(options?: ShipClientOptions);
235
+ protected abstract resolveInitialConfig(options: ShipClientOptions): any;
236
+ protected abstract loadFullConfig(): Promise<void>;
237
+ protected abstract processInput(input: DeployInput, options: DeploymentOptions): Promise<StaticFile[]>;
238
+ /**
239
+ * Ensure full initialization is complete - called lazily by resources
240
+ */
241
+ protected ensureInitialized(): Promise<void>;
242
+ /**
243
+ * Ping the API server to check connectivity
244
+ */
245
+ ping(): Promise<boolean>;
246
+ /**
247
+ * Deploy project (convenience shortcut to ship.deployments.create())
248
+ */
249
+ deploy(input: DeployInput, options?: DeploymentOptions): Promise<Deployment>;
250
+ /**
251
+ * Get current account information (convenience shortcut to ship.account.get())
252
+ */
253
+ whoami(): Promise<_shipstatic_types.Account>;
254
+ /**
255
+ * Get deployments resource (environment-specific)
256
+ */
257
+ get deployments(): DeploymentResource;
258
+ /**
259
+ * Get aliases resource
260
+ */
261
+ get aliases(): AliasResource;
262
+ /**
263
+ * Get account resource
264
+ */
265
+ get account(): AccountResource;
266
+ /**
267
+ * Get keys resource
268
+ */
269
+ get keys(): KeysResource;
148
270
  }
149
- interface KeysResource {
150
- create: () => Promise<{
151
- apiKey: string;
152
- }>;
271
+
272
+ /**
273
+ * @file Shared configuration logic for both environments.
274
+ */
275
+
276
+ type Config = PlatformConfig;
277
+ /**
278
+ * Universal configuration resolver for all environments.
279
+ * This is the single source of truth for config resolution.
280
+ */
281
+ declare function resolveConfig(userOptions?: ShipClientOptions, loadedConfig?: Partial<ShipClientOptions>): {
282
+ apiUrl: string;
283
+ apiKey?: string;
284
+ deployToken?: string;
285
+ };
286
+ /**
287
+ * Merge deployment options with client defaults.
288
+ * This is shared logic used by both environments.
289
+ */
290
+ declare function mergeDeployOptions(options: DeploymentOptions, clientDefaults: ShipClientOptions): DeploymentOptions;
291
+
292
+ interface MD5Result {
293
+ md5: string;
153
294
  }
295
+ /**
296
+ * Unified MD5 calculation that delegates to environment-specific handlers
297
+ */
298
+ declare function calculateMD5(input: Blob | Buffer | string): Promise<MD5Result>;
154
299
 
155
300
  /**
156
- * Processes Node.js file and directory paths into an array of StaticFile objects ready for deploy.
157
- * Now uses the simplified, declarative approach suggested in the feedback.
301
+ * Utility functions for string manipulation.
302
+ */
303
+ /**
304
+ * Simple utility to pluralize a word based on a count.
305
+ * @param count The number to determine pluralization.
306
+ * @param singular The singular form of the word.
307
+ * @param plural The plural form of the word.
308
+ * @param includeCount Whether to include the count in the returned string. Defaults to true.
309
+ * @returns A string with the count and the correctly pluralized word.
310
+ */
311
+ declare function pluralize(count: number, singular: string, plural: string, includeCount?: boolean): string;
312
+
313
+ /**
314
+ * List of directory names considered as junk
158
315
  *
159
- * @param paths - File or directory paths to scan and process.
160
- * @param options - Processing options (basePath, preserveDirs).
161
- * @returns Promise resolving to an array of StaticFile objects.
162
- * @throws {ShipClientError} If called outside Node.js or if fs/path modules fail.
316
+ * Files within these directories (at any level in the path hierarchy) will be excluded.
317
+ * The comparison is case-insensitive for cross-platform compatibility.
318
+ *
319
+ * @internal
163
320
  */
164
- declare function processFilesForNode(paths: string[], options?: DeploymentOptions): Promise<StaticFile[]>;
321
+ declare const JUNK_DIRECTORIES: readonly ["__MACOSX", ".Trashes", ".fseventsd", ".Spotlight-V100"];
322
+ /**
323
+ * Filters an array of file paths, removing those considered junk
324
+ *
325
+ * A path is filtered out if either:
326
+ * 1. The basename is identified as junk by the 'junk' package (e.g., .DS_Store, Thumbs.db)
327
+ * 2. Any directory segment in the path matches an entry in JUNK_DIRECTORIES (case-insensitive)
328
+ *
329
+ * All path separators are normalized to forward slashes for consistent cross-platform behavior.
330
+ *
331
+ * @param filePaths - An array of file path strings to filter
332
+ * @returns A new array containing only non-junk file paths
333
+ */
334
+ declare function filterJunk(filePaths: string[]): string[];
165
335
 
166
336
  /**
167
- * Processes browser files (FileList or File[]) into an array of StaticFile objects ready for deploy.
168
- * Calculates MD5, filters junk files, and applies automatic path optimization.
337
+ * @file Deploy path optimization - the core logic that makes Ship deployments clean and intuitive.
338
+ * Automatically strips common parent directories to create clean deployment URLs.
339
+ */
340
+ /**
341
+ * Represents a file ready for deployment with its optimized path
342
+ */
343
+ interface DeployFile {
344
+ /** The clean deployment path (e.g., "assets/style.css") */
345
+ path: string;
346
+ /** Original filename */
347
+ name: string;
348
+ }
349
+ /**
350
+ * Core path optimization logic.
351
+ * Transforms messy local paths into clean deployment paths.
169
352
  *
170
- * @param browserFiles - FileList or File[] to process for deploy.
171
- * @param options - Processing options including pathDetect for automatic path optimization.
172
- * @returns Promise resolving to an array of StaticFile objects.
173
- * @throws {ShipClientError} If called outside a browser or with invalid input.
353
+ * @example
354
+ * Input: ["dist/index.html", "dist/assets/app.js"]
355
+ * Output: ["index.html", "assets/app.js"]
356
+ *
357
+ * @param filePaths - Raw file paths from the local filesystem
358
+ * @param options - Path processing options
174
359
  */
175
- declare function processFilesForBrowser(browserFiles: FileList | File[], options?: DeploymentOptions): Promise<StaticFile[]>;
360
+ declare function optimizeDeployPaths(filePaths: string[], options?: {
361
+ flatten?: boolean;
362
+ }): DeployFile[];
176
363
 
177
364
  /**
178
365
  * @file Environment detection utilities for the Ship SDK.
@@ -193,73 +380,89 @@ type ExecutionEnvironment = 'browser' | 'node' | 'unknown';
193
380
  * @internal
194
381
  */
195
382
  declare function __setTestEnvironment(env: ExecutionEnvironment | null): void;
383
+ /**
384
+ * Gets the current effective execution environment.
385
+ *
386
+ * This function first checks if a test environment override is active via {@link __setTestEnvironment}.
387
+ * If not, it detects the actual environment (Node.js, browser, or unknown).
388
+ *
389
+ * @returns The current execution environment: 'browser', 'node', or 'unknown'.
390
+ * @public
391
+ */
392
+ declare function getENV(): ExecutionEnvironment;
393
+
394
+ /**
395
+ * @file Manages loading and validation of client configuration.
396
+ * This module uses `cosmiconfig` to find and load configuration from various
397
+ * file sources (e.g., `.shiprc`, `package.json`) and environment variables.
398
+ * Configuration values are validated using Zod schemas.
399
+ */
196
400
 
197
401
  /**
198
- * Ship SDK Client - Universal class-based interface for both Node.js and browser environments.
402
+ * Simplified configuration loading prioritizing environment variables.
403
+ * Only loads file config if environment variables are not set.
404
+ * Only available in Node.js environments.
199
405
  *
200
- * Authentication Options:
201
- * ```
406
+ * @param configFile - Optional specific config file path to load
407
+ * @returns Configuration object with loaded values
408
+ * @throws {ShipInvalidConfigError} If the configuration is invalid.
409
+ */
410
+ declare function loadConfig(configFile?: string): Promise<Partial<ShipClientOptions>>;
411
+
412
+ /**
413
+ * @file Platform configuration management for the Ship SDK.
414
+ * Implements fail-fast dynamic configuration with mandatory API fetch.
415
+ */
416
+
417
+ /**
418
+ * Set the current config (called after fetching from API)
419
+ */
420
+ declare function setConfig(config: ConfigResponse): void;
421
+ /**
422
+ * Get current config - throws if not initialized (fail-fast approach)
423
+ * @throws {ShipError.config} If configuration hasn't been fetched from API
424
+ */
425
+ declare function getCurrentConfig(): ConfigResponse;
426
+
427
+ /**
428
+ * Processes Node.js file and directory paths into an array of StaticFile objects ready for deploy.
429
+ * Uses corrected logic to properly handle common parent directory stripping.
430
+ *
431
+ * @param paths - File or directory paths to scan and process.
432
+ * @param options - Processing options (pathDetect, etc.).
433
+ * @returns Promise resolving to an array of StaticFile objects.
434
+ * @throws {ShipClientError} If called outside Node.js or if fs/path modules fail.
435
+ */
436
+ declare function processFilesForNode(paths: string[], options?: DeploymentOptions): Promise<StaticFile[]>;
437
+
438
+ /**
439
+ * @file Ship SDK for Node.js environments with full file system support.
440
+ */
441
+
442
+ /**
443
+ * Ship SDK Client for Node.js environments.
444
+ *
445
+ * Provides full file system access, configuration file loading,
446
+ * and environment variable support.
447
+ *
448
+ * @example
449
+ * ```typescript
202
450
  * // Authenticated deployments with API key
203
451
  * const ship = new Ship({ apiKey: "ship-xxxx" });
204
452
  *
205
453
  * // Single-use deployments with deploy token
206
454
  * const ship = new Ship({ deployToken: "token-xxxx" });
207
- * ```
208
455
  *
209
- * Automatically detects the environment and handles Node.js and browser deploys directly.
210
- * In Node.js environments, loads configuration from files and environment variables.
211
- * In browser environments, uses only the provided options.
456
+ * // Deploy a directory
457
+ * await ship.deploy('./dist');
458
+ * ```
212
459
  */
213
- declare class Ship {
214
- private http;
215
- private environment;
216
- private readonly clientOptions;
217
- private initPromise;
218
- private _deployments;
219
- private _aliases;
220
- private _account;
221
- private _keys;
460
+ declare class Ship extends Ship$1 {
461
+ #private;
222
462
  constructor(options?: ShipClientOptions);
223
- /**
224
- * Ensure full initialization is complete - called lazily by resources
225
- */
226
- private ensureInitialized;
227
- /**
228
- * Helper method to create initialization callback for resources
229
- */
230
- private getInitCallback;
231
- /**
232
- * Initialize config from file/env and platform config from API
233
- */
234
- private initializeConfig;
235
- /**
236
- * Ping the API server to check connectivity
237
- */
238
- ping(): Promise<boolean>;
239
- /**
240
- * Deploy project (convenience shortcut to ship.deployments.create())
241
- */
242
- deploy(input: DeployInput, options?: DeploymentOptions): Promise<Deployment>;
243
- /**
244
- * Get current account information (convenience shortcut to ship.account.get())
245
- */
246
- whoami(): Promise<_shipstatic_types.Account>;
247
- /**
248
- * Get deployments resource (environment-specific)
249
- */
250
- get deployments(): DeploymentResource;
251
- /**
252
- * Get aliases resource
253
- */
254
- get aliases(): AliasResource;
255
- /**
256
- * Get account resource
257
- */
258
- get account(): AccountResource;
259
- /**
260
- * Get keys resource
261
- */
262
- get keys(): KeysResource;
463
+ protected resolveInitialConfig(options: ShipClientOptions): any;
464
+ protected loadFullConfig(): Promise<void>;
465
+ protected processInput(input: DeployInput, options: DeploymentOptions): Promise<StaticFile[]>;
263
466
  }
264
467
 
265
- export { type AccountResource, type AliasResource, type ApiDeployOptions, type DeployInput, type DeploymentOptions, type DeploymentResource, type KeysResource, type ProgressStats, Ship, type ShipClientOptions, type StaticFile, __setTestEnvironment, Ship as default, processFilesForBrowser, processFilesForNode };
468
+ export { type ApiDeployOptions, ApiHttp, type Config, type DeployFile, type DeploymentOptions, type ExecutionEnvironment, JUNK_DIRECTORIES, type MD5Result, type ProgressStats, Ship, type ShipClientOptions, __setTestEnvironment, calculateMD5, createAccountResource, createAliasResource, createDeploymentResource, createKeysResource, Ship as default, filterJunk, getCurrentConfig, getENV, loadConfig, mergeDeployOptions, optimizeDeployPaths, pluralize, processFilesForNode, resolveConfig, setConfig };