-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to extend adapter? #70
Comments
You can extend the Storage by extending the public API. But then you would have to implement the new features that you've added to the local adapter to all other adapters as well otherwise you break the abstraction layer which is the main feature and "raison d'être" of this library. What you can do to use the functionality that you've added to AdapterLocal is: const configuration = {
type: StorageType.LOCAL,
directory: "path/to/directory",
};
const storage = new Storage(configuration);
const adapter = storage.getAdapter() as AdapterLocal;
adapter.yourMethod();
// or:
(storage.adapter as AdapterLocal).yourMethod(); |
Alternately you could use the adapter stand-alone: const configuration = {
type: StorageType.LOCAL,
directory: "path/to/directory",
};
const adapter = new AdapterLocal();
adapter.yourMethod(); You can use the full API on an adapter instance directly. You'd only be missing |
You can extend the protected method |
But then I'd lose the storage abstraction, with callers needing to handle the different storage backends. What I was hoping was to override the However, as the |
You don't need to override protected async _getFileAsURL(
bucketName: string,
fileName: string,
options: Options
): Promise<ResultObject> {
// or use any other key as long as it is not used by the other adapters
if (options.publicUrl === true) {
try {
// your code that generates a public url
return { value: yourPublicUrl, error: null };
} catch (e) {
return { value: null, error: e.message };
}
} else {
try {
const p = path.join(this._config.directory, bucketName, fileName);
try {
await fs.promises.access(p);
} catch (e) {
return { value: null, error: e };
}
if (options.withoutDirectory) {
return { value: path.join(bucketName, fileName), error: null };
}
return { value: p, error: null };
} catch (e) {
return { value: null, error: e.message };
}
}
} By using the This is also guaranteed because the export interface Options {
[id: string]: any; // eslint-disable-line
} |
While it is not necessary to write a complete new version of the local adapter for the functionality that you want to add, you can create custom adapters, see this part of the readme. |
I'm not a dev and have no experience with Node.js, which is probably showing. Thank you very much for taking the time to respond to me nonetheless! Once I've extended Essentially, I was hoping I could do something like this: var adapters = [MyLocalAdapter, S3Adapter]
const storage = Storage(storageConfigUrl, availableAdapters=adapters) Or something along those lines, with my changes constrained to the new MyLocalAdapter, without having to use a rewritten |
Once you've extended The Storage class is merely a shim around the adapter API. This means that you can load your custom adapter in a Storage instance as long as your adapter fully implements the adapter API. The Storage class has only 2 extra methods: const configLocal = {
type: StorageType.LOCAL,
...
};
const configS3 = {
type: StorageType.S3,
...
};
const availableAdapters = [configLocal, configS3];
const storage = new Storage(availableAdapters[0]);
// then later on in your code if you need to read/write file to S3:
storage.switchAdapter(availableAdapters[1]); Could you share the code of your extended |
I close this issue for now. Let me know if there is anything I can do to help you by reopening this issue. |
I have extended
AdapterLocal
to add a method to generate public URLs.However, there is no convenient way to use this adapter with the Storage class that I can see.
There's a few ways I can think of to make this work, but the easiest might simply be to make
_adapter
protected. Let me know your thoughtsThe text was updated successfully, but these errors were encountered: