Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed file download responses documented as JSON and made the download examples check the response

Damian Legawiecdf49316c5734
diff --git a/docs/api-reference/admin.yaml b/docs/api-reference/admin.yaml
index 1f1dd0b9eb..b53b9cf35c 100644
--- a/docs/api-reference/admin.yaml
+++ b/docs/api-reference/admin.yaml
@@ -9872,7 +9872,6 @@ paths:
'200':
description: CSV file
content:
- application/json: {}
text/csv:
schema:
type: string
@@ -12429,7 +12428,6 @@ paths:
'200':
description: CSV template
content:
- application/json: {}
text/csv:
schema:
type: string
@@ -12720,7 +12718,6 @@ paths:
'200':
description: CSV file
content:
- application/json: {}
text/csv:
schema:
type: string
@@ -39515,8 +39512,6 @@ paths:
responses:
'200':
description: document streamed
- content:
- application/json: {}
'422':
description: no document attached
content:
diff --git a/docs/api-reference/seller.yaml b/docs/api-reference/seller.yaml
index d45e1835c6..41187b5a3b 100644
--- a/docs/api-reference/seller.yaml
+++ b/docs/api-reference/seller.yaml
@@ -13705,6 +13705,7 @@ paths:
const response = await fetch(`https://your-store.com${submission.file_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
const link = Object.assign(document.createElement('a'), {
href: url,
@@ -17567,6 +17568,7 @@ paths:
const response = await fetch(`https://your-store.com${exp.download_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
const link = Object.assign(document.createElement('a'), {
href: url,
@@ -19005,12 +19007,18 @@ paths:
if (label.download_url) {
// Labels are served through the API, so fetch with the session's token
- // and seller, then open the file for printing:
+ // and seller, then drive the browser download for printing:
const response = await fetch(`https://your-store.com${label.download_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
- window.open(url)
+ const link = Object.assign(document.createElement('a'), {
+ href: url,
+ download: `label-${label.tracking_number ?? label.id}.${label.format ?? 'pdf'}`,
+ })
+ link.click()
+ URL.revokeObjectURL(url)
}
parameters:
- name: X-Spree-Seller-Id
@@ -19022,7 +19030,18 @@ paths:
'200':
description: label file streamed
content:
- application/json: {}
+ application/pdf:
+ schema:
+ type: string
+ format: binary
+ image/png:
+ schema:
+ type: string
+ format: binary
+ application/octet-stream:
+ schema:
+ type: string
+ format: binary
"/api/v3/seller/tracking_carriers":
get:
summary: List tracking carriers
@@ -20776,6 +20795,7 @@ paths:
const response = await fetch(`https://your-store.com${productImport.original_file_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
const link = Object.assign(document.createElement('a'), {
href: url,
@@ -20794,7 +20814,6 @@ paths:
'200':
description: CSV file
content:
- application/json: {}
text/csv:
schema:
type: string
diff --git a/packages/seller-sdk/examples/exports/download.ts b/packages/seller-sdk/examples/exports/download.ts
index 7941e1dc7a..a92dac6eb0 100644
--- a/packages/seller-sdk/examples/exports/download.ts
+++ b/packages/seller-sdk/examples/exports/download.ts
@@ -20,6 +20,7 @@ if (exp.download_url) {
const response = await fetch(`https://your-store.com${exp.download_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
const link = Object.assign(document.createElement('a'), {
href: url,
diff --git a/packages/seller-sdk/examples/imports/download.ts b/packages/seller-sdk/examples/imports/download.ts
index 7a45dd8cf3..5a9ce7843f 100644
--- a/packages/seller-sdk/examples/imports/download.ts
+++ b/packages/seller-sdk/examples/imports/download.ts
@@ -15,6 +15,7 @@ if (productImport.original_file_url) {
const response = await fetch(`https://your-store.com${productImport.original_file_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
const link = Object.assign(document.createElement('a'), {
href: url,
diff --git a/packages/seller-sdk/examples/order-labels/download.ts b/packages/seller-sdk/examples/order-labels/download.ts
index 7a53e617b9..6b1f5b8300 100644
--- a/packages/seller-sdk/examples/order-labels/download.ts
+++ b/packages/seller-sdk/examples/order-labels/download.ts
@@ -15,11 +15,17 @@ const label = await client.orders.fulfillments.labels.get(
if (label.download_url) {
// Labels are served through the API, so fetch with the session's token
- // and seller, then open the file for printing:
+ // and seller, then drive the browser download for printing:
const response = await fetch(`https://your-store.com${label.download_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
- window.open(url)
+ const link = Object.assign(document.createElement('a'), {
+ href: url,
+ download: `label-${label.tracking_number ?? label.id}.${label.format ?? 'pdf'}`,
+ })
+ link.click()
+ URL.revokeObjectURL(url)
}
// endregion:example
diff --git a/packages/seller-sdk/examples/requirement-submissions/download.ts b/packages/seller-sdk/examples/requirement-submissions/download.ts
index dc83d95272..b4d9b68ae1 100644
--- a/packages/seller-sdk/examples/requirement-submissions/download.ts
+++ b/packages/seller-sdk/examples/requirement-submissions/download.ts
@@ -17,6 +17,7 @@ if (submission?.file_url) {
const response = await fetch(`https://your-store.com${submission.file_url}`, {
headers: { Authorization: 'Bearer eyJhbGciOi...', 'X-Spree-Seller-Id': 'sel_UkLWZg9DAJ' },
})
+ if (!response.ok) throw new Error(`Download failed with status ${response.status}`)
const url = URL.createObjectURL(await response.blob())
const link = Object.assign(document.createElement('a'), {
href: url,
diff --git a/spree/api/spec/integration/spree/api/v3/seller/fulfillments_spec.rb b/spree/api/spec/integration/spree/api/v3/seller/fulfillments_spec.rb
index d77b595399..060c26710d 100644
--- a/spree/api/spec/integration/spree/api/v3/seller/fulfillments_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/seller/fulfillments_spec.rb
@@ -527,6 +527,8 @@ RSpec.describe 'Seller Fulfillments API', type: :request, swagger_doc: 'api-refe
let(:fulfillment_id) { fulfillment.prefixed_id }
let(:id) { create(:shipping_label, :uploaded, :with_file, owner: fulfillment, store: store).prefixed_id }
+ schema type: :string, format: :binary
+
run_test! do |response|
expect(response.body).to start_with('%PDF')
end
diff --git a/spree/api/spec/swagger_helper.rb b/spree/api/spec/swagger_helper.rb
index a0cf672cb2..79d30bc4e6 100644
--- a/spree/api/spec/swagger_helper.rb
+++ b/spree/api/spec/swagger_helper.rb
@@ -325,9 +325,12 @@ RSpec.configure do |config|
next unless respond_to?(:response) && response.present? && response.body.present?
begin
+ # Parsed before the entry is created: a file download would otherwise
+ # leave an empty JSON media type on a response that never returns JSON.
+ example_body = JSON.parse(response.body, symbolize_names: true)
content = response_metadata[:content] ||= {}
content['application/json'] ||= {}
- content['application/json'][:example] = JSON.parse(response.body, symbolize_names: true)
+ content['application/json'][:example] = example_body
rescue JSON::ParserError
# Skip if response body is not valid JSON
end