-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): upload files directly via alexandria API
BREAKING CHANGE: Requires alexandria backend v3.0.0-beta.3
- Loading branch information
Showing
10 changed files
with
106 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ApplicationAdapter from "./application"; | ||
|
||
export default class FileAdapter extends ApplicationAdapter { | ||
ajaxOptions(url, type, options) { | ||
const ajaxOptions = super.ajaxOptions(url, type, options); | ||
|
||
if (type === "PUT") { | ||
// Use PATCH instead of PUT for updating records | ||
ajaxOptions.type = "PATCH"; | ||
ajaxOptions.method = "PATCH"; | ||
} | ||
|
||
if (type === "PUT" || type === "POST") { | ||
// Remove content type for updating and creating records so the content | ||
// type will be defined by the passed form data | ||
delete ajaxOptions.headers["content-type"]; | ||
} | ||
|
||
return ajaxOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import JSONSerializer from "@ember-data/serializer/json-api"; | ||
|
||
/* | ||
* If pagination is enabled in the backend, the response format will be changed. | ||
* The response data will be wrapped in a `results` object. | ||
* This would need some configurable normalizer functionality to work. | ||
*/ | ||
export default class FileSerializer extends JSONSerializer { | ||
// If we don't do this, Ember will interpret the `meta` property in the single | ||
// response as meta object and omit it from the attributes. | ||
extractMeta() {} | ||
|
||
// Disable root key serialization since we want to send plain form data | ||
serializeIntoHash = null; | ||
|
||
serialize(snapshot) { | ||
const { name, variant, content } = snapshot.attributes(); | ||
|
||
const formData = new FormData(); | ||
|
||
formData.append("name", name); | ||
formData.append("variant", variant); | ||
formData.append("document", snapshot.belongsTo("document")?.id); | ||
|
||
if (content instanceof File) { | ||
formData.append("content", content); | ||
} | ||
|
||
return formData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ApplicationAdapter from "./application"; | ||
|
||
export default class FileAdapter extends ApplicationAdapter { | ||
ajaxOptions(url, type, options) { | ||
const ajaxOptions = super.ajaxOptions(url, type, options); | ||
|
||
if (type === "PUT") { | ||
// Use PATCH instead of PUT for updating records | ||
ajaxOptions.type = "PATCH"; | ||
ajaxOptions.method = "PATCH"; | ||
} | ||
|
||
if (type === "PUT" || type === "POST") { | ||
// Remove content type for updating and creating records so the content | ||
// type will be defined by the passed form data | ||
delete ajaxOptions.headers["content-type"]; | ||
} | ||
|
||
return ajaxOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { setupTest } from "dummy/tests/helpers"; | ||
import { module, test } from "qunit"; | ||
|
||
module("Unit | Adapter | file", function (hooks) { | ||
setupTest(hooks); | ||
|
||
// Replace this with your real tests. | ||
test("it exists", function (assert) { | ||
const adapter = this.owner.lookup("adapter:file"); | ||
assert.ok(adapter); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters