Add activestorage

This commit is contained in:
Cameron Bothner
2019-01-12 14:01:07 -05:00
parent 3a69de74e2
commit 08500a1978
4 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import * as ActiveStorage from 'activestorage';
ActiveStorage.start();
const delegate: ActiveStorage.DirectUploadDelegate = {
directUploadWillCreateBlobWithXHR(xhr) {
console.log(xhr.status);
},
directUploadWillStoreFileWithXHR(xhr) {
console.log(xhr.status);
},
};
const d = new ActiveStorage.DirectUpload(
new File([], 'blank.txt'),
'/rails/active_storage/direct_uploads',
delegate
);
d.create((error, blob) => {
if (error) {
console.log(error.message);
} else {
const { byte_size, checksum, content_type, filename, signed_id } = blob;
console.log({ byte_size, checksum, content_type, filename, signed_id });
}
});

33
types/activestorage/index.d.ts vendored Normal file
View File

@@ -0,0 +1,33 @@
// Type definitions for ActiveStorage 5.2
// Project: https://github.com/rails/rails/tree/master/activestorage/app/javascipt
// Definitions by: Cameron Bothner <https://github.com/cbothner>
// Definitions: https://github.com/cbothner/DefinitelyTyped
// TypeScript Version: 2.1
export as namespace ActiveStorage
export function start(): void;
export class DirectUpload {
id: number;
file: File;
url: string;
constructor(file: File, url: string, delegate: DirectUploadDelegate)
create(callback: (error: Error, blob: Blob) => void): void;
}
export interface DirectUploadDelegate {
directUploadWillCreateBlobWithXHR?: (xhr: XMLHttpRequest) => void;
directUploadWillStoreFileWithXHR?: (xhr: XMLHttpRequest) => void;
}
export interface Blob {
byte_size: number;
checksum: string;
content_type: string;
filename: string;
signed_id: string;
}

View File

@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "activestorage-tests.ts"]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }