Skip to content

Commit 46f46f5

Browse files
jordanverasamyodacrem
authored andcommitted
use SQLITE_DATABASE resource type for createStagedUpload
fix tests and lint
1 parent 51c1ebb commit 46f46f5

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

packages/store/src/apis/admin/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ShopDetailsQuery, ShopDetailsQueryVariables} from '../../cli/api/gr
1414
export async function createStagedUploadAdmin(
1515
storeFqdn: string,
1616
input: StagedUploadInput[],
17+
version?: string,
1718
): Promise<StagedUploadsCreateMutation> {
1819
const adminSession = await ensureAuthenticatedAdmin(storeFqdn)
1920

@@ -23,6 +24,7 @@ export async function createStagedUploadAdmin(
2324
variables: {
2425
input,
2526
},
27+
version,
2628
})
2729
}
2830

packages/store/src/cli/api/graphql/admin/generated/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,10 @@ export type StagedUploadTargetGenerateUploadResource =
232232
* [fileCreate mutation](https://shopify.dev/api/admin-graphql/latest/mutations/fileCreate).
233233
*/
234234
| 'VIDEO'
235+
/**
236+
* A SQLite database file.
237+
*
238+
* For example, after uploading the database file, you can use the
239+
* bulkDataImportStart mutation to import the data into a Shopify store.
240+
*/
241+
| 'SQLITE_DATABASE'

packages/store/src/services/store/utils/file-uploader.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('FileUploader', () => {
2626
stagedTargets: [
2727
{
2828
url: 'https://upload.shopify.com/staged',
29-
resourceUrl: 'https://shopify.com/resource/123',
29+
resourceUrl: 'https://shopify.com/resource/123/',
3030
parameters: [
3131
{name: 'key', value: 'uploads/123'},
3232
{name: 'policy', value: 'policy123'},
@@ -52,17 +52,21 @@ describe('FileUploader', () => {
5252
test('should successfully upload a valid SQLite file', async () => {
5353
const result = await fileUploader.uploadSqliteFile(mockFilePath, mockStoreFqdn)
5454

55-
expect(result).toBe('https://shopify.com/resource/123')
55+
expect(result).toBe('https://shopify.com/resource/123/uploads/123')
5656
expect(readFileSync).toHaveBeenCalledWith(mockFilePath)
57-
expect(createStagedUploadAdmin).toHaveBeenCalledWith(mockStoreFqdn, [
58-
{
59-
resource: 'FILE',
60-
filename: 'database.sqlite',
61-
mimeType: 'application/x-sqlite3',
62-
httpMethod: 'POST',
63-
fileSize: '1024',
64-
},
65-
])
57+
expect(createStagedUploadAdmin).toHaveBeenCalledWith(
58+
mockStoreFqdn,
59+
[
60+
{
61+
resource: 'SQLITE_DATABASE',
62+
filename: 'database.sqlite',
63+
mimeType: 'application/x-sqlite3',
64+
httpMethod: 'POST',
65+
fileSize: '1024',
66+
},
67+
],
68+
'unstable',
69+
)
6670
expect(fetch).toHaveBeenCalledWith('https://upload.shopify.com/staged', {
6771
method: 'POST',
6872
body: expect.any(FormData),
@@ -103,14 +107,14 @@ describe('FileUploader', () => {
103107
})
104108

105109
test('should throw error when file is too large', async () => {
106-
const largeSize = 30 * 1024 * 1024
110+
const largeSize = 400 * 1024 * 1024
107111
vi.mocked(fileSize).mockResolvedValue(largeSize)
108112

109113
const promise = fileUploader.uploadSqliteFile(mockFilePath, mockStoreFqdn)
110114
await expect(promise).rejects.toThrow(ValidationError)
111115
await expect(promise).rejects.toMatchObject({
112116
code: ErrorCodes.FILE_TOO_LARGE,
113-
params: {filePath: mockFilePath, fileSize: '30MB', maxSize: '20MB'},
117+
params: {filePath: mockFilePath, fileSize: '400MB', maxSize: '300MB'},
114118
})
115119
})
116120

packages/store/src/services/store/utils/file-uploader.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import {fileExistsSync, fileSize, isDirectory, readFileSync} from '@shopify/cli-
55
import {outputDebug} from '@shopify/cli-kit/node/output'
66

77
export class FileUploader {
8-
private readonly MAX_FILE_SIZE = 20 * 1024 * 1024
8+
private readonly MAX_FILE_SIZE = 300 * 1024 * 1024
99

1010
async uploadSqliteFile(filePath: string, storeFqdn: string): Promise<string> {
1111
await this.validateSqliteFile(filePath)
1212

1313
const fileBuffer = readFileSync(filePath)
1414
const sizeOfFile = await fileSize(filePath)
1515
const uploadInput: StagedUploadInput = {
16-
resource: 'FILE',
16+
resource: 'SQLITE_DATABASE',
1717
filename: 'database.sqlite',
1818
mimeType: 'application/x-sqlite3',
1919
httpMethod: 'POST',
2020
fileSize: sizeOfFile.toString(),
2121
}
2222

23-
const stagedUploadResponse = await createStagedUploadAdmin(storeFqdn, [uploadInput])
23+
const stagedUploadResponse = await createStagedUploadAdmin(storeFqdn, [uploadInput], 'unstable')
2424

2525
if (!stagedUploadResponse.stagedUploadsCreate?.stagedTargets?.length) {
2626
throw new OperationError('upload', ErrorCodes.STAGED_UPLOAD_FAILED, {
@@ -42,6 +42,15 @@ export class FileUploader {
4242
})
4343
}
4444

45+
const stagedUploadKeyParam = parameters.find((parameter) => parameter.name === 'key')
46+
if (!stagedUploadKeyParam) {
47+
throw new OperationError('upload', ErrorCodes.STAGED_UPLOAD_FAILED, {
48+
reason: 'Missing key parameter in staged upload target',
49+
})
50+
}
51+
52+
const finalResourceUrl = resourceUrl + stagedUploadKeyParam.value
53+
4554
const formData = new FormData()
4655

4756
parameters.forEach((param) => {
@@ -63,7 +72,7 @@ export class FileUploader {
6372
})
6473
}
6574

66-
return resourceUrl
75+
return finalResourceUrl
6776
}
6877

6978
private async validateSqliteFile(filePath: string): Promise<void> {

0 commit comments

Comments
 (0)