npm package: @smartlayers/smlayer
Description: SMLAYER JS is a library built with Parcel JS that allows developers to easily make use of [Smart Layers](https://app.smartlayers.io)-generated pipelines and models within their applications.
Latest version: 0.1.16

## Overview

SMLAYER JS is a library built with Parcel JS that allows developers to easily make use of [Smart Layers](https://app.smartlayers.io)-generated pipelines and models within their applications.

## Build Dist

To create build files in the dist folder, run the command below.

```
npx parcel build
```

## Installing SMLAYER

To install `smlayer` within your project, run:

```
npm install "@smartlayers/smlayer"
```

## Using SMLAYER

For now, the following methods are available in smlayer:

- **`submit(formData)`** : This method allows you to upload a file to a pipeline for processing. To use it, you should have [form-data](https://www.npmjs.com/package/form-data) installed within your project.

  <sub> **Arguments:** </sub>

  - Arg1 (formData): FormData allows you to create readable "multipart/form-data" streams. You should construct a form using FormData(), append a field called "file" to it, then pass it.

  <sub> **Return value:** </sub>

  ```
     {
      data: [
         {
          "request_log": "string",
          "machine_result": { },
          "result": { },
          "result_reviewed": true,
          "status": "Pending",
          "_id": "stringstringstringstrin",
          "pipeline_id": "string",
          "file_object": {},
          "ocr_tokens": [],
          "processed_file_object": {},
          "thumbnail_file_object": {},
          "created_at": "string",
          "updated_at": "string",
          "attributes": { }
         }
       ],
       status: 200,
       statusText: 'OK',
       headers: {},
       config: {},
       request: {}
      }
  ```

- **`sumbitSync(formData)`** : This allows upload, and is obligated to wait until your result is completely processed, before returning a response. It also relies on `form-data` for parsing files.

  <sub> **Arguments:** </sub>

  - Arg1 (formData): FormData allows you to create readable "multipart/form-data" streams. You should construct a form using FormData(), append a field called "file" to it, then pass it.

  <sub> **Return value:** </sub>

  ```
     {
      "request_log": "string",
      "machine_result": { },
      "result": { },
      "result_reviewed": true,
      "status": "Pending",
      "_id": "stringstringstringstrin",
      "pipeline_id": "string",
      "file_object": {},
      "ocr_tokens": [],
      "processed_file_object": {},
      "thumbnail_file_object": {},
      "created_at": "string",
      "updated_at": "string",
      "attributes": { }
     }
  ```

- **`submitFromPath(filePath)`** : Another method of upload that simply requires you to just pass in the path to the file in question. It can also take in a URL.

  <sub> **Arguments:** </sub>

  - Arg1 (filePath): filePath is the path to the file you wish to upload on your local machine, or a url to an external file. It shiuld be a string.

  <sub> **Return value:** </sub>

  ```
     [
      {
      "request_log": "string",
      "machine_result": { },
      "result": { },
      "result_reviewed": true,
      "status": "Pending",
      "_id": "stringstringstringstrin",
      "pipeline_id": "string",
      "file_object": {},
      "ocr_tokens": [],
      "processed_file_object": {},
      "thumbnail_file_object": {},
      "created_at": "string",
      "updated_at": "string",
      "attributes": { }
      }
     ]
  ```

- **`getAllResults()`** : Get all the results in a particular pipeline.

  <sub> **Return value:** </sub>

  ```
     {
      "results": [
       {
        "request_log": "string",
        "machine_result": { },
        "result": { },
        "result_reviewed": true,
        "status": "Pending",
        "_id": "stringstringstringstrin",
        "pipeline_id": "string",
        "file_object": {},
        "ocr_tokens": [],
        "processed_file_object": {},
        "thumbnail_file_object": {},
        "created_at": "string",
        "updated_at": "string",
        "attributes": { }
       }
      ],
      "total_pending_pipeline_results": 0,
      "total_results": 0
     }
  ```

- **`getSingleResult(resultId)`** : Get a single result from the pipeline based on its ID.

  <sub> **Arguments:** </sub>

  - Arg1 (resultId): resultId is the id of the record you're trying to get. It should be a string.

  <sub> **Return value:** </sub>

  ```
     {
      "request_log": "string",
      "machine_result": { },
      "result": { },
      "result_reviewed": true,
      "status": "Pending",
      "_id": "stringstringstringstrin",
      "pipeline_id": "string",
      "file_object": {},
      "ocr_tokens": [],
      "processed_file_object": {},
      "thumbnail_file_object": {},
      "created_at": "string",
      "updated_at": "string",
      "attributes": { }
     }
  ```

## Methods in play

### submit(formData)

```js
import { Pipeline } from "smlayer";
import FormData from "form-data";
import * as fs from "fs";

let pipe = new Pipeline(pipelineId, apiKey);
let formData = new FormData();

let file = fs.createReadStream(_fileLocation_);

formData.append("file", file);
let res = await pipe.submit(formData);

console.log(res.data);
```

**NOTE :**

- Files to be uploaded via FormData are to be parsed with the name, "file".
- For uploading files from external sources, you may want to use [submitFromPath()](https://github.com/SmartLayers/smlayer/edit/master/README.md#submitfrompathfilepath) or the code below, if you insist.

```js
const response = await axios.get(_url_, {
  responseType: "stream",
});
let file = response.data;
```

### submitSync(formData)

```js
import { Pipeline } from "smlayer";
import FormData from "form-data";
import * as fs from "fs";

let pipe = new Pipeline(pipelineId, apiKey);
let formData = new FormData();

let file = fs.createReadStream(_fileLocation_);

formData.append("file", file);
let res = await pipe.submitSync(formData);

console.log(res);
```

### submitFromPath(filePath)

```js
import { Pipeline } from "smlayer";

let pipe = new Pipeline(pipelineId, apiKey);

let res = await pipe.submitFromPath(_filePath_);

console.log(res);
```

### getAllResults()

```js
import { Pipeline } from "smlayer";

let pipe = new Pipeline(pipelineId, apiKey);

let res = await pipe.getAllResults();

console.log(res);
```

### getSingleResult(resultId)

```js
import { Pipeline } from "smlayer";

let pipe = new Pipeline(pipelineId, apiKey);

let res = await pipe.getSingleResult(resultId);

console.log(res);
```

## SMARTLAYERS Platform

https://app.smartlayers.io