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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Expose the asset source through the Admin API
Damian Legawiecaf2c89c8bdfa
diff --git a/packages/admin-sdk/src/admin-client.ts b/packages/admin-sdk/src/admin-client.tsindex 18d3c7118e..172d133d1b 100644--- a/packages/admin-sdk/src/admin-client.ts+++ b/packages/admin-sdk/src/admin-client.ts@@ -138,6 +138,7 @@ import type {DeliveryProfileParams,DeliveryZoneParams,DigitalAssetCreateParams,+ DigitalAssetProvider,DigitalAssetUpdateParams,DirectUploadCreateParams,ExchangeCreateParams,@@ -942,6 +943,21 @@ export class AdminClient {delete: (productId: string, id: string, options?: RequestOptions): Promise<void> =>this.request<void>('DELETE', `/products/${productId}/digital_assets/${id}`, options),++ /**+ * The sources a merchant can pick for a new asset: the uploaded-file+ * default plus any host-registered provider. A host with only the default+ * gets a single-entry list.+ */+ providers: (+ productId: string,+ options?: RequestOptions,+ ): Promise<{ data: DigitalAssetProvider[] }> =>+ this.request<{ data: DigitalAssetProvider[] }>(+ 'GET',+ `/products/${productId}/digital_assets/providers`,+ options,+ ),},variants: {diff --git a/packages/admin-sdk/src/params.ts b/packages/admin-sdk/src/params.tsindex e19f38bbb2..54243b636d 100644--- a/packages/admin-sdk/src/params.ts+++ b/packages/admin-sdk/src/params.ts@@ -668,9 +668,22 @@ export interface ProductMediaInput {variant_ids?: Array<string>}+/** A selectable digital-asset source, from the `providers` discovery endpoint. */+export interface DigitalAssetProvider {+ /** Class name to send as `provider_type`. */+ type: string+ /** Human-readable label. */+ name: string+ /** Whether picking this source requires uploading a file. */+ requires_attachment: boolean+}+export interface DigitalAssetCreateParams {- /** Signed blob id from a `private: true` direct upload. */- signed_id: string+ /** Signed blob id from a `private: true` direct upload. Required for a file+ * asset (the default source); omitted for a provider-backed asset. */+ signed_id?: string+ /** Provider class name. Omit for an uploaded file (the default source). */+ provider_type?: string/** Defaults to the product's default variant when omitted. */variant_id?: string/** Null (or omitted) means the store's download settings apply. */@@ -681,6 +694,7 @@ export interface DigitalAssetCreateParams {export interface DigitalAssetUpdateParams {/** Replaces the file for everyone who already bought it. */signed_id?: string+ provider_type?: stringvariant_id?: stringauthorized_clicks?: number | nullauthorized_days?: number | nulldiff --git a/packages/admin-sdk/src/types/generated/DigitalAsset.ts b/packages/admin-sdk/src/types/generated/DigitalAsset.tsindex cde48396fb..c004d0652a 100644--- a/packages/admin-sdk/src/types/generated/DigitalAsset.ts+++ b/packages/admin-sdk/src/types/generated/DigitalAsset.ts@@ -1,4 +1,4 @@-// Typelizer digest 3db1dfd6fed0d31eafcb3777caba36cc+// Typelizer digest 50bce2576c8eedf0f0e048a65ad8f67c//// DO NOT MODIFY: This file was automatically generated by Typelizer.@@ -10,8 +10,10 @@ interface DigitalAsset {authorized_clicks: number | null;authorized_days: number | null;byte_size: number | null;+ provider_type: string | null;created_at: string;updated_at: string;+ provider_name: string;download_url: string | null;effective_authorized_clicks: number;effective_authorized_days: number;diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rbindex e88c07ad61..34d7bf27b9 100644--- a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb@@ -26,6 +26,25 @@ module Spreesuperend+ # GET /api/v3/admin/products/:product_id/digital_assets/providers+ #+ # The sources a merchant can pick for a new asset: the uploaded-file+ # default plus any host-registered provider. Drives the dashboard's+ # source selector; a host with only the default sees just "upload".+ def providers+ authorize! :create, model_class++ data = Spree.digital_asset_providers.map do |provider_class|+ {+ type: provider_class.to_s,+ name: provider_class.provider_name,+ requires_attachment: provider_class.requires_attachment?+ }+ end++ render json: { data: data }+ end+protecteddef model_class@@ -63,8 +82,10 @@ module Spree# mass-assigning it would let a caller point an asset at a variant# of any product, in any store. signed_id likewise names a blob# rather than a column, so it is attached separately.+ # provider_type is mass-assignable: the model validates it names a+ # registered provider, so an unknown value is a 422, not a hazard.def permitted_params- params.permit(:authorized_clicks, :authorized_days)+ params.permit(:authorized_clicks, :authorized_days, :provider_type)endprivatediff --git a/spree/api/app/serializers/spree/api/v3/admin/digital_asset_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/digital_asset_serializer.rbindex 2a2c1f06f6..77116fe9fa 100644--- a/spree/api/app/serializers/spree/api/v3/admin/digital_asset_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/digital_asset_serializer.rb@@ -10,18 +10,27 @@ module Spreeauthorized_days: [:number, nullable: true],effective_authorized_clicks: :number,effective_authorized_days: :number,+ provider_type: [:string, nullable: true],+ provider_name: :string,download_url: [:string, nullable: true]- attributes :authorized_clicks, :authorized_days, :byte_size+ attributes :authorized_clicks, :authorized_days, :byte_size, :provider_typeattributes created_at: :iso8601, updated_at: :iso8601+ # Human-readable source for the admin listing. Blank provider_type is+ # the uploaded-file default, so this reads "File" there.+ attribute :provider_name do |digital_asset|+ digital_asset.provider_class.provider_name+ end+# Lets a merchant check what they actually uploaded. Short-lived and# admin-only: this is the file itself, not a customer's download# grant, so it carries no counter and spends nobody's allowance.- #- # Nil outside a request: the Disk service needs a host, which only a- # controller can supply, and event payloads are serialized without one.+ # Nil for a provider-backed asset (no file to preview) and outside a+ # request (the Disk service needs a host only a controller supplies).attribute :download_url do |digital_asset, params|+ next nil unless digital_asset.downloadable?+store = params&.dig(:store) || Spree::Current.store || digital_asset.storebegindigital_asset.download_url(expires_in: store.preferred_digital_asset_link_expire_time.seconds)diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex 1559213286..fee2e0a083 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -318,7 +318,11 @@ Spree::Core::Engine.add_routes doendresources :media, controller: 'media', only: [:index, :create, :update, :destroy]resources :digital_assets, controller: 'products/digital_assets',- only: [:index, :create, :show, :update, :destroy]+ only: [:index, :create, :show, :update, :destroy] do+ collection do+ get :providers+ end+ endend# Digital download grants — read and reset only; they are created bydiff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rbindex 085de48df6..93a9c0c354 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb@@ -224,4 +224,51 @@ RSpec.describe Spree::Api::V3::Admin::Products::DigitalAssetsController, type: :expect(json_response['variant_id']).to eq(variant.prefixed_id)endend++ describe 'GET #providers' do+ it 'lists the registered sources, including the File default' do+ get :providers, params: { product_id: product.prefixed_id }++ expect(response).to have_http_status(:ok)+ types = json_response['data'].map { |p| p['type'] }+ expect(types).to include('Spree::DigitalAssetProvider::File')+ file = json_response['data'].find { |p| p['type'] == 'Spree::DigitalAssetProvider::File' }+ expect(file['requires_attachment']).to be(true)+ end+ end++ describe 'provider_type' do+ let(:stub_provider) do+ Class.new(Spree::DigitalAssetProvider::Base) do+ def self.requires_attachment? = false+ def deliver(_link, expires_in:) = Spree::DigitalDelivery.new(inline_value: 'X')+ end+ end++ before do+ stub_const('Spree::DigitalAssetProvider::Stub', stub_provider)+ Spree.digital_asset_providers << stub_provider+ end++ after { Spree.digital_asset_providers.delete(stub_provider) }++ it 'creates a provider-backed asset with no file' do+ post :create, params: {+ product_id: product.prefixed_id,+ provider_type: 'Spree::DigitalAssetProvider::Stub'+ }, as: :json++ expect(response).to have_http_status(:created)+ expect(json_response['provider_type']).to eq('Spree::DigitalAssetProvider::Stub')+ end++ it 'rejects an unregistered provider_type' do+ post :create, params: {+ product_id: product.prefixed_id,+ provider_type: 'Spree::DigitalAssetProvider::Nope'+ }, as: :json++ expect(response).to have_http_status(:unprocessable_content)+ end+ endend