Skip to content

Commit

Permalink
readme: update path lib->src
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjclark committed Jul 21, 2023
1 parent a4293ea commit 3fd064a
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,57 +62,72 @@ please make one and assign yourself.
the 'Review checklist'. Leave any notes for the reviewer in the 'Details'
section.

4. Mark the pull request as ready for review, and assign @stuartc or @taylordowns2000.
4. Mark the pull request as ready for review, and assign @stuartc or
@taylordowns2000.

### Working with adaptors

#### 1. Setup
#### 1. Setup

- Create a copy of the adaptor [template](packages/template), and rename it.
`cp -R packages/template packages/youradaptorname`

- Open package.json, and update the `name`, `description` and `build` from `template` to your `youradaptorname`. Update the version.
- Open package.json, and update the `name`, `description` and `build` from
`template` to your `youradaptorname`. Update the version.

- Update the top-level changelog comment `# @openfn/language-template` to `# @openfn/language-youradaptorname` and delete the remaining example content. Delete ast.json, then run `pnpm clean` (this will remove the dist, types and docs folders).
- Update the top-level changelog comment `# @openfn/language-template` to
`# @openfn/language-youradaptorname` and delete the remaining example content.
Delete ast.json, then run `pnpm clean` (this will remove the dist, types and
docs folders).

- Update your adaptor's README.md to use your new adaptor name (we'll come back to this later to update the sample operation)
- Update your adaptor's README.md to use your new adaptor name (we'll come back
to this later to update the sample operation)

#### 2. Create a job with the operation you'd like to add, and run it so it fails

- Create a tmp folder where you can test your operation manually by running a job: `mkdir tmp`. Note: this tmp folder is included in .gitignore, so it will not be pushed to github.
- Create a job file `touch tmp/job.js` which calls the operation you would like to add. For example, if you were adding an operation which gets the weather forecast, it might look something like this:
- Create a tmp folder where you can test your operation manually by running a
job: `mkdir tmp`. Note: this tmp folder is included in .gitignore, so it will
not be pushed to github.
- Create a job file `touch tmp/job.js` which calls the operation you would like
to add. For example, if you were adding an operation which gets the weather
forecast, it might look something like this:

```
getTodaysWeather(25.52, 13.41);
```

- Run your job with `openfn tmp/job.js -O -ma youradaptorname`
- Run your job with `openfn tmp/job.js -O -ma youradaptorname`

`-O` will output to your console
`-O` will output to your console

`-m` will run the job from the monorepo (make sure your monorepo
path is set OPENFN_ADAPTORS_REPO=...)
`-m` will run the job from the monorepo (make sure your monorepo
path is set OPENFN_ADAPTORS_REPO=...)

`-a` this will specify the adaptor to run your job with
`-a` this will specify the adaptor to run your job with

- The job should fail - we haven't build the adaptor yet! Let's go do that

#### 3. Create your adaptor function
- Head to `lib/Adaptor.js`, and define your first function.
#### 3. Create your adaptor function

- Head to `src/Adaptor.js`, and define your first function.

Adaptor functions are invoked with several arguments, and return a function
which accepts and returns state.

Adaptor functions are invoked with several arguments, and return a function which accepts and returns state.
They should look something like this:

They should look something like this:
```js
export function yourFunctionName(arguments) {
return state => {
// logic
// logic
return state;
};
}
```

The logic is where you will prepare your API request. Build the URL, passing in any arguments if needed.
Next, return the fetch request, making sure you set the Authorization, passing in any secrets from `state.configuration`.
The logic is where you will prepare your API request. Build the URL, passing in
any arguments if needed. Next, return the fetch request, making sure you set the
Authorization, passing in any secrets from `state.configuration`.

```js
export function getTodaysWeather(latitude = 25.52, longitude = 13.41) {
Expand All @@ -134,13 +149,14 @@ export function getTodaysWeather(latitude = 25.52, longitude = 13.41) {

- Define the configuration schema in `configuration-schema.json`

This defines required parameters and their expected values
for configuring the adaptor's authentication and authorization settings.
This defines required parameters and their expected values for configuring the
adaptor's authentication and authorization settings.

1. Open the configuration file of the newly copied template,
`configuration-schema.json`.
2. Specify the required parameters and their data types (these will be any values in state.configuration you are using in your adaptor function). Include descriptions
that explain the purpose and usage of each parameter.
2. Specify the required parameters and their data types (these will be any
values in state.configuration you are using in your adaptor function).
Include descriptions that explain the purpose and usage of each parameter.
3. If certain parameters are optional, clearly indicate their optional status
and provide default values or instructions for their usage.
4. Validate the configuration against the schema to ensure that the provided
Expand All @@ -150,23 +166,25 @@ export function getTodaysWeather(latitude = 25.52, longitude = 13.41) {

[Here's an example of a configuration schema](packages/template/configuration-schema.json)

#### 4. Test it manually

#### 4. Test it manually
- Make sure to run build again
- Run your job `openfn tmp/job.js -O -ma youradaptorname`
- Make sure it returns what you expect
- You can delete this folder once you have finished testing your job manually

#### 5. Write your unit tests

Import your newly defined function
Import your newly defined function

```
import { functionName } from '../src/Adaptor.js';
```

Describe your test, define state, call your operation (follow the example below to see how state should be passed to it).
Describe your test, define state, call your operation (follow the example
below to see how state should be passed to it).

Make an assertion to check the result.
Make an assertion to check the result.

```
it('should xyz', async () => {
Expand All @@ -183,13 +201,13 @@ export function getTodaysWeather(latitude = 25.52, longitude = 13.41) {

Run `pnpm test` to check your test is passing.

Make sure to test your function with different arguments and values.
Make sure to test your function with different arguments and values.

#### 6. Add docs and write the tests

- Include [JSDoc](https://jsdoc.app/) comments to provide a clear and
comprehensive explanation of the adaptor function's purpose, parameters, return values, and
usage examples
comprehensive explanation of the adaptor function's purpose, parameters,
return values, and usage examples
- Update the adaptor's `README.md` to include a sample operation

## Changesets
Expand Down

0 comments on commit 3fd064a

Please sign in to comment.