npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@allanferencz/file-adapters

v1.0.0

Published

<!--[meta] section: api subSection: field-adapters title: File Adapters [meta]-->

Downloads

5

Readme

File Adapters

The File field type can support files hosted in a range of different contexts, e.g. in the local filesystem, or on a cloud based file server.

Different contexts are supported by different file adapters. This package contains the built-in file adapters supported by KeystoneJS.

LocalFileAdapter

Usage

const { LocalFileAdapter } = require('@keystonejs/file-adapters');

const fileAdapter = new LocalFileAdapter({
  /*...config */
});

Config

| Option | Type | Default | Description | | ------------- | ---------- | -------------- | --------------------------------------------------------------------------------------------------------------------------- | | src | String | Required | The path where uploaded files will be stored on the server. | | path | String | Value of src | The path from which requests for files will be served from the server. | | getFilename | Function | null | Function taking a { id, originalFilename } parameter. Should return a string with the name for the uploaded file on disk. |

Note: src and path may be the same.

Methods

delete

Takes an object with a file key representing the filename and deletes that file on disk. This can be combined with hooks to implement delete-on-file-change and delete-on-list-delete functionality:

const { File } = require('@keystonejs/fields');

const fileAdapter = new LocalFileAdapter({
  src: './files',
  path: '/files',
});

keystone.createList('UploadTest', {
  fields: {
    file: {
      type: File,
      adapter: fileAdapter,
      hooks: {
        beforeChange: ({ existingItem = {} }) => fileAdapter.delete(existingItem),
      },
    },
  },
  hooks: {
    afterDelete: ({ existingItem = {} }) => fileAdapter.delete(existingItem),
  },
});

CloudinaryFileAdapter

Usage

const { CloudinaryAdapter } = require('@keystonejs/file-adapters');

const fileAdapter = new CloudinaryAdapter({
  /*...config */
});

Config

| Option | Type | Default | Description | | ----------- | -------- | ----------- | ----------- | | cloudName | String | Required | | | apiKey | String | Required | | | apiSecret | String | Required | | | folder | String | undefined | |

Methods

delete

Takes an object with an id key representing the public file ID and deletes that file on the server.

S3FileAdapter

const { S3Adapter } = require('@keystonejs/file-adapters');

const CF_DISTRIBUTION_ID = 'cloudfront-distribution-id';
const S3_PATH = 'uploads';

const fileAdapter = new S3Adapter({
  accessKeyId: 'ACCESS_KEY_ID',
  secretAccessKey: 'SECRET_ACCESS_KEY',
  region: 'us-west-2',
  bucket: 'bucket-name',
  folder: S3_PATH,
  publicUrl: ({ id, filename, _meta }) =>
    `https://${CF_DISTRIBUTION_ID}.cloudfront.net/${S3_PATH}/${filename}`,
  s3Options: {
    apiVersion: '2006-03-01',
  },
  uploadParams: ({ filename, id, mimetype, encoding }) => ({
    Metadata: {
      keystone_id: id,
    },
  }),
});

| Option | Type | Default | Description | | ----------------- | ----------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | accessKeyId | String | Required | AWS access key ID | | secretAccessKey | String | Required | AWS secret access key | | region | String | Required | AWS region | | bucket | String | Required | S3 bucket name | | folder | String | Required | Upload folder from root of bucket | | getFilename | Function | null | Function taking a { id, originalFilename } parameter. Should return a string with the name for the uploaded file on disk. | | publicUrl | Function | | By default the publicUrl returns a url for the S3 bucket in the form https://{bucket}.s3.amazonaws.com/{key}/{filename}. This will only work if the bucket is configured to allow public access. | | s3Options | Object | undefined | For available options refer to the AWS S3 API | | uploadParams | Object|Function | {} | A config object or function returning a config object to be passed with each call to S3.upload. For available options refer to the AWS S3 upload API. |

Methods

delete

Deletes the provided file in the S3 bucket. Takes a file object (such as the one returned in file field hooks) and an optional options argument for overriding S3.deleteObject options. Options Bucket and Key are set by default. For available options refer to the AWS S3 deleteObject API.

const deleteParams = {
  BypassGovernanceRetention: true,
};

keystone.createList('Document', {
  fields: {
    file: {
      type: File,
      adapter: fileAdapter,
      hooks: {
        beforeChange: ({ existingItem }) => {
          if (existingItem && existingItem.file) {
            fileAdapter.delete(existingItem.file, deleteParams);
          }
        },
      },
    },
  },
  hooks: {
    afterDelete: ({ existingItem }) => {
      if (existingItem.file) {
        fileAdapter.delete(existingItem.file, deleteParams);
      }
    },
  },
});

AzureFileAdapter

const { AzureAdapter } = require('@keystonejs/file-adapters');


const fileAdapter = new AzureAdapter({
  accountName: `STORAGE_ACCOUNT_NAME`,
  accountKey: `STORAGE_ACCOUNT_ACCESS_KEY`,
  containerName: 'CONTAINER_NAME'
});

| Option | Type | Default | Description | | ----------------- | ----------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | accountName | String | Required | Azure Storage Account Name | | accountKey | String | Required | Azure Storage Account Key | | containerName | String | Required | Azure Storage Container Name

Methods

delete

Deletes the provided file in the Azure Blob Storage Account. Takes a file object (such as the one returned in file field hooks).

keystone.createList('Users', {
  fields: {
    avatar: {
      type: File,
      adapter: fileAdapter,
      hooks: {
        beforeChange: ({ existingItem }) => {
          if (existingItem && existingItem.avatar) {
            fileAdapter.delete(existingItem.avatar);
          }
        },
      },
    },
  },
  hooks: {
    afterDelete: ({ existingItem }) => {
      if (existingItem.avatar) {
        fileAdapter.delete(existingItem.avatar);
      }
    },
  },
});