1.0.6 • Published 5 days ago

@openfn/language-sftp v1.0.6

Weekly downloads
-
License
LGPLv3
Repository
github
Last release
5 days ago

Language SFTP

Language Pack for building expressions and operations to work with SFTP servers.

Documentation

Configuration

View all the required and optional properties for state.configuration in the official configuration-schema definition.

List the content of a directory

alterState(state => {
  return list('/path/To/Directory')(state).then(response => {
    console.log(`There are ${response.data.length} files.`);
    return response;
  });
});

sample getCSV expression

getCSV('path/to/file.csv', 'utf8', {
  quote: 'off',
  delimiter: ';',
  noheader: true,
  filter: {
    type: 'startsWith',
    key: 'field1',
    value: 'JO',
  },
});

A more complex example that breaks up the CSV file into multiple payloads for quicker processing.

fn(state => {
  return list('/')(state).then(state => {
    const targetNames = [
      'exportContacts', //example fileName
    ];
    console.log(`Fetching files: ${targetNames}`);
    const files = state.data
      .filter(file => file.name.split('.')[1] === 'csv')
      .filter(file =>
        targetNames.some(targetName =>
          file.name.toLowerCase().includes(targetName)
        )
      );

    if (files.length === 0) console.log('No new CSV files found.');
    return { ...state, data: {}, files };
  });
});

each(
  '$.files[*]',
  fn(state => {
    const { configuration, data } = state;

    return getCSV(`/${data.name}`)(state).then(async state => {
      const headers = state.data
        .shift()
        .split(';')
        .map(h => (h = h.replace(/"/g, '')));

      function toObject(item) {
        const values = item.split(';');

        return Object.fromEntries(
          headers.map((k, i) => {
            return values[i]
              ? [k, values[i].replace(/"/g, '')]
              : [k, values[i]];
          })
        );
      }

      let countInbox = 0;

      //to post CSV data as individual Messages to OpenFn Inbox
      const postToInbox = async data => {
        countInbox++;

        console.log(`Sending request ${countInbox} to inbox`);

        await new Promise(resolve => setTimeout(resolve, 200));

        await http.post({
          url: configuration.openfnInboxUrl,
          data: data,
          maxContentLength: Infinity,
          maxBodyLength: Infinity,
        })(state);
      };

      //To split up into multiple, smaller payloads before send to OpenFn Inbox
      const chunkSize = 500;

      console.log(
        state.data.length,
        'rows will be sent in',
        Math.ceil(state.data.length / chunkSize),
        'requests of',
        chunkSize,
        'rows each.'
      );

      while (state.data.length > 0) {
        console.log('data.length', state.data.length);
        await postToInbox({
          fileName: data.name,
          fileType: data.name.split('-')[0],
          uploadDate: new Date(data.modifyTime).toISOString(),
          json: state.data.splice(0, chunkSize).map(toObject),
        });
      }

      return { configuration, references: [], data: {} };
    });
  })
);

sample putCSV expression

This function converts JSON to CSV and post to a server

putCSV('/some/path/to_file.csv', 'utf8', { delimiter: ';', noheader: true });

Get JSON from FTP server

getJSON('path/to/file.json', 'utf8');

Custom request to an http endpoint

This adaptor exports http from language-common. Here, we outline the usage in order to make custom requests to an endpoint. It returns a promise

alterState(state => {
  return http
    .post({ url: 'yourURL', data: { name: 'Mamadou' } })(state)
    .then(response => {
      // do something with response;
      return response;
    });
});

Development

Clone the repo, run pnpm install.

Run tests using pnpm run test or pnpm run test:watch

Build the project using pnpm build.

To build the docs for this repo, pnpm build docs

1.0.6

5 days ago

1.0.5

5 days ago

1.0.4

1 month ago

1.0.3

1 month ago

1.0.2

7 months ago

1.0.1

7 months ago

0.8.8

8 months ago

1.0.0

7 months ago

0.8.5

10 months ago

0.8.4

10 months ago

0.8.7

8 months ago

0.8.6

9 months ago

0.8.3

11 months ago

0.8.1

11 months ago

0.7.2

12 months ago

0.7.1

12 months ago

0.8.2

11 months ago

0.7.3

12 months ago

0.7.0

12 months ago

0.6.9

1 year ago

0.6.8

1 year ago

0.6.7

1 year ago

0.6.6

1 year ago

0.6.5

1 year ago

0.6.4

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.5.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.4.0

3 years ago

0.3.3

3 years ago