@forge/os 1.1.0 → 1.1.2-next.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.
@@ -42,14 +42,11 @@ describe('ObjectStoreClient', () => {
42
42
  const path = 'api/v1/objects/1';
43
43
  const response = await osClient['sendRequest'](path);
44
44
  expect(mockFetch).toHaveBeenCalledWith(path, {
45
- redirect: 'follow',
46
- headers: {
47
- ...DEFAULT_HEADERS
48
- }
45
+ redirect: 'follow'
49
46
  });
50
47
  expect(response).toBe(mockResponse);
51
48
  });
52
- it('should handle requests that override the default headers', async () => {
49
+ it('should handle requests that set headers', async () => {
53
50
  const mockResponse = { ok: true, status: 200 };
54
51
  mockFetch.mockResolvedValue(mockResponse);
55
52
  const path = 'api/v1/objects/1';
@@ -59,47 +56,44 @@ describe('ObjectStoreClient', () => {
59
56
  redirect: 'follow',
60
57
  method: 'GET',
61
58
  headers: {
62
- ...DEFAULT_HEADERS,
63
59
  'Content-Type': 'application/octet-stream'
64
60
  }
65
61
  });
66
62
  expect(response).toBe(mockResponse);
67
63
  });
68
64
  });
69
- describe('echo', () => {
70
- it('should send a GET request to the echo endpoint', async () => {
71
- const mockResponse = {
72
- ok: true,
73
- status: 200,
74
- text: createMockBody(JSON.stringify({ message: 'Blåhaj' }))
75
- };
65
+ describe('put', () => {
66
+ it('should send a PUT request with the correct headers and body', async () => {
67
+ const mockResponse = { ok: true, status: 201, text: createMockBody(null) };
76
68
  mockFetch.mockResolvedValue(mockResponse);
77
- const response = await osClient.echo();
78
- expect(mockFetch).toHaveBeenCalledWith('api/v1/echo', {
79
- method: 'GET',
69
+ const objectId = '1';
70
+ const buffer = Buffer.from('hello world');
71
+ await osClient.put(objectId, buffer);
72
+ expect(mockFetch).toHaveBeenCalledWith('api/v1/objects/1', {
73
+ method: 'PUT',
80
74
  redirect: 'follow',
81
75
  headers: {
82
- ...DEFAULT_HEADERS
83
- }
76
+ ...DEFAULT_HEADERS,
77
+ 'Content-Type': 'application/octet-stream'
78
+ },
79
+ body: buffer
84
80
  });
85
- expect(mockResponse.status).toBe(200);
81
+ expect(mockResponse.status).toBe(201);
86
82
  expect(mockResponse.text).toHaveBeenCalled();
87
- expect(response).toEqual({ message: 'Blåhaj' });
88
83
  });
89
- });
90
- describe('put', () => {
91
- it('should send a PUT request with the correct headers and body', async () => {
84
+ it('should send a PUT request with the ttl headers and body', async () => {
92
85
  const mockResponse = { ok: true, status: 201, text: createMockBody(null) };
93
86
  mockFetch.mockResolvedValue(mockResponse);
94
87
  const objectId = '1';
95
88
  const buffer = Buffer.from('hello world');
96
- await osClient.put(objectId, buffer);
89
+ await osClient.put(objectId, buffer, 100);
97
90
  expect(mockFetch).toHaveBeenCalledWith('api/v1/objects/1', {
98
91
  method: 'PUT',
99
92
  redirect: 'follow',
100
93
  headers: {
101
94
  ...DEFAULT_HEADERS,
102
- 'Content-Type': 'application/octet-stream'
95
+ 'Content-Type': 'application/octet-stream',
96
+ 'ttl-seconds': '100'
103
97
  },
104
98
  body: buffer
105
99
  });
@@ -121,7 +115,7 @@ describe('ObjectStoreClient', () => {
121
115
  method: 'GET',
122
116
  redirect: 'follow',
123
117
  headers: {
124
- ...DEFAULT_HEADERS
118
+ Accept: 'application/json'
125
119
  }
126
120
  });
127
121
  expect(mockResponse.status).toBe(200);
@@ -139,7 +133,7 @@ describe('ObjectStoreClient', () => {
139
133
  method: 'DELETE',
140
134
  redirect: 'follow',
141
135
  headers: {
142
- ...DEFAULT_HEADERS
136
+ Accept: 'application/json'
143
137
  }
144
138
  });
145
139
  expect(mockResponse.status).toBe(204);
@@ -156,11 +150,10 @@ describe('ObjectStoreClient', () => {
156
150
  mockFetch.mockResolvedValue(mockResponse);
157
151
  const objectId = '1';
158
152
  const response = await osClient.download(objectId);
159
- expect(mockFetch).toHaveBeenCalledWith('api/v1/objects/1', {
153
+ expect(mockFetch).toHaveBeenCalledWith('api/v1/objects/1/download', {
160
154
  method: 'GET',
161
155
  redirect: 'follow',
162
156
  headers: {
163
- ...DEFAULT_HEADERS,
164
157
  Accept: 'application/octet-stream'
165
158
  }
166
159
  });
package/out/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { os } from './os';
2
2
  import { errorCodes } from './errorCodes';
3
3
  import { ForgeError } from './errors';
4
- export { errorCodes, os, ForgeError };
4
+ import { ObjectReference, VersionsList } from './types';
5
+ export { errorCodes, os, ForgeError, ObjectReference, VersionsList };
5
6
  export default os;
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,CAAC;AAEtC,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,eAAe,EAAE,YAAY,EAAE,CAAC;AAErE,eAAe,EAAE,CAAC"}
package/out/os.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /// <reference types="node" />
2
+ import { ObjectReference, VersionsList } from './types';
2
3
  export declare class ObjectStoreClient {
3
4
  private sendRequest;
4
- private storageApi;
5
- echo(): Promise<string | null>;
6
- put(objectId: string, buffer: Buffer): Promise<void>;
7
- get<DataType>(objectId: string): Promise<DataType | null>;
5
+ private requestJson;
6
+ put(objectId: string, buffer: Buffer, ttlSeconds?: number): Promise<ObjectReference | null>;
7
+ get(objectId: string): Promise<ObjectReference | null>;
8
8
  delete(objectId: string): Promise<void>;
9
- listVersions(objectId: string): Promise<unknown>;
9
+ listVersions(objectId: string): Promise<VersionsList | null>;
10
10
  download(objectId: string): Promise<Buffer | null>;
11
11
  }
12
12
  export declare const os: ObjectStoreClient;
package/out/os.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"os.d.ts","sourceRoot":"","sources":["../src/os.ts"],"names":[],"mappings":";AAgBA,qBAAa,iBAAiB;YAEd,WAAW;YAYX,UAAU;IA8BX,IAAI;IAIJ,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQpD,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAIzD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvC,YAAY,CAAC,QAAQ,EAAE,MAAM;IAI7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAOhE;AAED,eAAO,MAAM,EAAE,mBAA0B,CAAC"}
1
+ {"version":3,"file":"os.d.ts","sourceRoot":"","sources":["../src/os.ts"],"names":[],"mappings":";AAIA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAQxD,qBAAa,iBAAiB;YAEd,WAAW;YASX,WAAW;IAqCZ,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAa3F,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAUtD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAU5D,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAehE;AAED,eAAO,MAAM,EAAE,mBAA0B,CAAC"}
package/out/os.js CHANGED
@@ -10,67 +10,68 @@ var ValidHttpMethod;
10
10
  ValidHttpMethod["GET"] = "GET";
11
11
  ValidHttpMethod["DELETE"] = "DELETE";
12
12
  })(ValidHttpMethod || (ValidHttpMethod = {}));
13
- const DEFAULT_HEADERS = {
14
- Accept: 'application/json',
15
- 'Content-Type': 'application/json'
16
- };
17
13
  class ObjectStoreClient {
18
14
  async sendRequest(path, options) {
19
15
  return await (0, api_1.__fetchProduct)({ provider: 'app', remote: 'os' })(path, {
20
16
  ...options,
21
17
  redirect: 'follow',
22
- headers: {
23
- ...DEFAULT_HEADERS,
24
- ...options?.headers
25
- }
18
+ headers: options?.headers
26
19
  });
27
20
  }
28
- async storageApi(method, path, headers, body) {
21
+ async requestJson(method, path, headers, body) {
29
22
  const response = await this.sendRequest(path, {
30
23
  method,
31
24
  headers,
32
25
  body
33
26
  });
34
- await (0, error_handling_1.checkResponseError)(response);
35
- if (headers?.Accept === 'application/octet-stream') {
36
- return response.arrayBuffer().then((buffer) => Buffer.from(buffer));
27
+ if (response.status === 404) {
28
+ return null;
37
29
  }
30
+ await (0, error_handling_1.checkResponseError)(response);
38
31
  const responseText = await response.text();
39
- if (responseText) {
40
- try {
41
- return JSON.parse(responseText);
42
- }
43
- catch (error) {
44
- throw new errors_1.ForgeObjectStoreError(`Unexpected error. Response was not valid JSON: ${responseText}`);
45
- }
32
+ if (!responseText || responseText.trim().length === 0) {
33
+ return null;
34
+ }
35
+ try {
36
+ return JSON.parse(responseText);
37
+ }
38
+ catch (error) {
39
+ throw new errors_1.ForgeObjectStoreError(`Unexpected error. Response was not valid JSON: ${responseText}`);
46
40
  }
47
- return null;
48
- }
49
- async echo() {
50
- return this.storageApi(ValidHttpMethod.GET, `api/v1/echo`, { 'Content-Type': 'application/json' });
51
41
  }
52
- async put(objectId, buffer) {
42
+ async put(objectId, buffer, ttlSeconds) {
53
43
  const headers = {
54
- ...DEFAULT_HEADERS,
55
- 'Content-Type': 'application/octet-stream'
44
+ Accept: 'application/json',
45
+ 'Content-Type': 'application/octet-stream',
46
+ ...(ttlSeconds ? { 'ttl-seconds': ttlSeconds.toString() } : {})
56
47
  };
57
- await this.storageApi(ValidHttpMethod.PUT, `api/v1/objects/${objectId}`, headers, buffer);
48
+ return this.requestJson(ValidHttpMethod.PUT, `api/v1/objects/${objectId}`, headers, buffer);
58
49
  }
59
50
  async get(objectId) {
60
- return this.storageApi(ValidHttpMethod.GET, `api/v1/objects/${objectId}`, DEFAULT_HEADERS);
51
+ return this.requestJson(ValidHttpMethod.GET, `api/v1/objects/${objectId}`, {
52
+ Accept: 'application/json'
53
+ });
61
54
  }
62
55
  async delete(objectId) {
63
- await this.storageApi(ValidHttpMethod.DELETE, `api/v1/objects/${objectId}`, DEFAULT_HEADERS);
56
+ await this.requestJson(ValidHttpMethod.DELETE, `api/v1/objects/${objectId}`, { Accept: 'application/json' });
64
57
  }
65
58
  async listVersions(objectId) {
66
- return this.storageApi(ValidHttpMethod.GET, `api/v1/objects/${objectId}/versions`, DEFAULT_HEADERS);
59
+ return this.requestJson(ValidHttpMethod.GET, `api/v1/objects/${objectId}/versions`, {
60
+ Accept: 'application/json'
61
+ });
67
62
  }
68
63
  async download(objectId) {
69
- const headers = {
70
- ...DEFAULT_HEADERS,
71
- Accept: 'application/octet-stream'
72
- };
73
- return this.storageApi(ValidHttpMethod.GET, `api/v1/objects/${objectId}`, headers);
64
+ const response = await this.sendRequest(`api/v1/objects/${objectId}/download`, {
65
+ method: ValidHttpMethod.GET,
66
+ headers: {
67
+ Accept: 'application/octet-stream'
68
+ }
69
+ });
70
+ if (response.status === 404) {
71
+ return null;
72
+ }
73
+ await (0, error_handling_1.checkResponseError)(response);
74
+ return await response.arrayBuffer().then((buffer) => Buffer.from(buffer));
74
75
  }
75
76
  }
76
77
  exports.ObjectStoreClient = ObjectStoreClient;
package/out/types.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export interface ObjectReference {
2
+ key: string;
3
+ checksum: string;
4
+ size: number;
5
+ createdAt?: string;
6
+ currentVersion?: string;
7
+ }
8
+ export interface VersionsList {
9
+ versions: ObjectReference[];
10
+ }
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B"}
package/out/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forge/os",
3
- "version": "1.1.0",
3
+ "version": "1.1.2-next.0",
4
4
  "description": "Forge Object Store SDK",
5
5
  "author": "Atlassian",
6
6
  "license": "UNLICENSED",