@heroku/sf-fx-runtime-nodejs
TypeScript icon, indicating that this package has built-in type declarations

0.14.5 • Public • Published

Test workflow status

sf-fx-runtime-nodejs

Building

$ npm ci && npm run build && npm link

Create a function using a template

  • Install the sfdx cli via npm
  • Install the functions plugin sfdx plugins:install @salesforce/plugin-functions
  • Generate a project and a function:
$ cd ~/
$ sfdx generate:project -n myproject
$ cd myproject
$ sfdx generate:function --language=javascript --name=nodejsfunction

Using

$ sf-fx-runtime-nodejs serve ~/myproject/functions/nodejsfunction

Invoke with fake data

$ ./invoke.sh "localhost:8080" "{}"

Invoke against a scratch org

Generate a scratch org using a dev hub:

$ sfdx plugins:install @salesforce/plugin-functions
$ cat > /tmp/project-scratch-def.json << EOF
{
  "orgName": "My company",
  "edition": "Developer",
  "features": ["EnableSetPasswordInApi"],
  "settings": {
    "lightningExperienceSettings": {
      "enableS1DesktopEnabled": true
    },
    "mobileSettings": {
      "enableS1EncryptedStoragePref2": false
    }
  }
}
EOF
$ sfdx force:auth:web:login -d -a MyHub
$ sfdx force:org:create -s -f /tmp/project-scratch-def.json -a MyScratchOrg

Verify the org was created correctly:

$ sfdx force:org:list --all | grep MyScratchOrg
(U)  MyScratchOrg  test-<uuid>@example.com  <SFDC ID> Active   2021-08-02

Invoke your local function with your scratch org:

$ sfdx run:function -l http://localhost:8080 -p '{}' -o MyScratchOrg

Release

Bumping versions

In order to bump a version, update the CHANGELOG.md and the package.json. You will also need to tag the release. Run the following script:

$ npm run bump -- $VERSION

Note: the format should be x.y.z-ext. There is no need to include the v in the version number.

Create a pull request with the created changes. The branch can be named release-vx.y.z. In the PR subject, name it Release vx.y.z with the version release.

Creating a release

Once the release's pull request is merged, update your local main branch.

Make sure you've got access to release the NPM package:

$ npm access ls-packages @heroku | grep sf-fx-runtime-nodejs
  "@heroku/sf-fx-runtime-nodejs": "read-write"

You'll be asked for your NPM two-factor authentication code when releasing. To release, run:

$ npm run release

The script will build the relase, push it up to S3, create a git tag, and push up the release to GitHub.

After the package has been pushed, you'll need to release a new version of the buildpack in github.com/heroku/buildpacks-nodejs.

Example Functions

This library supports functions using both JavaScript Modules / ESM AND CommonJS / Node modules. Salesforce functions is transitioning from CommonJS / Node modules to JavaScript Modules / ESM, where newer functions will use the latter, older functions the former. Examples of each follow.

JavaScript Modules / ESM

package.json

{
  "name": "nodejs-example-function",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "yahoo-stock-prices": "^1.1.0"
  }
}

index.js

import yahooStockPrices from "yahoo-stock-prices";

export default function (event, context, logger) => {
    logger.info("I'm logging stuff!");
    return yahooStockPrices.getCurrentData("CRM");
}

Node Modules / CommonJS

package.json

{
  "name": "nodejs-example-function",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "yahoo-stock-prices": "^1.1.0"
  }
}

index.js

const yahooStockPrices = require("yahoo-stock-prices");

module.exports = (event, context, logger) => {
    logger.info("I'm logging stuff!");
    return yahooStockPrices.getCurrentData("CRM");
}

Dev

Install Dependencies

$ npm install

Linting and Formatting

You can lint files with eslint like so:

$ npm run lint

We also use prettier to auto-format code. You can run that with

$ npm run format:fix

Tests

This will start our wiremock server and run the mocha tests:

$ npm run test

To run the mocha tests without booting wiremock (say you are running your own):

$ npm run mocha

Mocks

We use wiremock to provide a fake salesforce API during test and local dev.

Start the mock server with:

$ npm run wiremock

You can view mappings on disk at mappings/ and on the server (if it is running) at http://localhost:8080/__admin.

Recording new mocks

Get the URL of your salesforce scratch org:

$ sfdx force:org:display -u MyScratchOrg | grep "Instance Url"
Instance Url     https://<my-url>-dev-ed.cs45.my.salesforce.com/

following the wiremock docs on recording boot your wiremock server and navigate to http://localhost:8080/__admin/recorder. In the browser enter the URL of your salesforce scratch org. Using the above example it would be https://<my-url>-dev-ed.cs45.my.salesforce.com/

Now write your test as you normally would and point your url at your wiremock port (http://localhost:8080). Any requests made to this port will be forwarded to your salesforce url. The response will be recorded an a "scratch mapping" automatically generated.

When you've recorded a scratch mapping you want to use, rename it something descriptive before committing it. Also edit the mapping to remove the default "ignoreExtraElements": true declaration in bodyPatterns as it's been a source of issues where wiremock should have failed, but did not.

Tip: You can focus one specific test by passing the -g flag into mocha along with the name of the test. For example to run only the "invalid version" test you could run:

$ npm run mocha -- -g "invalid version"

> sf-fx-runtime-nodejs@0.1.1-ea test
> mocha "-g" "invalid version"



  DataApi Class
    create()
      invalid version
        ✓ throws a not found error


  1 passing (50ms)

/@heroku/sf-fx-runtime-nodejs/

    Package Sidebar

    Install

    npm i @heroku/sf-fx-runtime-nodejs

    Weekly Downloads

    242

    Version

    0.14.5

    License

    ISC

    Unpacked Size

    504 kB

    Total Files

    43

    Last publish

    Collaborators

    • srishtigautam
    • aklimaite
    • jsullivandigs
    • julianduque-sfdc
    • cwallsf
    • msamineni
    • jeffrey.estrada
    • dlira-sf
    • ntonollier
    • daebumlee
    • tetsuya_
    • mttr
    • eleary
    • salesforce-releases
    • bhawnaheroku
    • revativelagada
    • dboston528
    • anujpandey001
    • sripad.kulkarni
    • chetankd10
    • rafaydin
    • kbaird-sf
    • cbaxtersf
    • ryoneyama
    • adirasanam
    • mengxiao.zhao
    • nrisaro
    • cmarcojos
    • adirksmeier
    • mgates-salesforce
    • ademusoyo
    • elzoona
    • christopher.jose
    • jondaniel-sfdc
    • dhagberg-sf
    • bencdenton
    • lsangalang2023
    • marcelsfdc
    • ccasey
    • joeybrown90sf
    • suppada
    • diana.doherty.sfdc
    • ryanbrainard
    • rsoerensen
    • jasoncummings-heroku
    • sconklin
    • brock-salesforce
    • tkoh-sfdc
    • gnetter
    • coreypurcell_salesforce
    • swapnilghose
    • stessema-sfc
    • jmonreal
    • brittany.jones
    • rstiltonsf
    • nilamuthu
    • sghaffar
    • cdwort-sfdc
    • agatpareek
    • aaronromeosfdc
    • kpremkumar
    • rchanda
    • 7ftz
    • xx-sfdc-npm
    • cv88
    • ekozilforce
    • umarsfdc
    • beanieboi
    • ndavidson-sf
    • mingzhi-liu1258
    • angelcampbell
    • mandeepsf
    • yann_ck
    • dasofiei
    • rajashri-gurusamy
    • sudarshanhiray
    • alex_heroku
    • sbosio_sf
    • sarahoh2468
    • justinwilaby-sf
    • cmcclung-npm-salesforce.com
    • tlowrimore
    • vish_heroku
    • paulaprado18
    • ptemporini
    • tcareysmith-sfdc
    • robinson.m
    • grundy.g
    • dkabanov1
    • cgfuh
    • rdagg-hero
    • k80bowman
    • sheax0r
    • dpark.heroku
    • afraidknot
    • daniel.bright
    • jdowning
    • abhinavkoul
    • joshwlewis-sfdc
    • viniljain
    • eblack
    • k.plenty
    • apadala
    • jhili!16
    • lsorstokke
    • rcross
    • kdreyeroren
    • dhammer25
    • tenharmsel_sf
    • dougmcinnes-sfdc
    • hillzbot2000
    • chap-heroku
    • michellejhlim
    • mbuschheroku
    • reidmix
    • beckychen
    • smrutisamanta
    • apoorvasriv
    • sfalan
    • p.shreya
    • sai_harshitha_neela
    • nlanatta
    • dreichenberg
    • vlazuka
    • mars
    • heroku-front-end
    • richatiwari
    • wchrisjohnson
    • the7oka
    • simonebonettisfita
    • lucaszhou
    • psalim
    • taylor.jones
    • angelayoung
    • mble-sfdc-2
    • ktsforce
    • kerry-bennett
    • pranamsharma
    • cory.steele
    • gkommineni
    • smuku
    • ykatz
    • dgallegossfdc
    • dzuelke-sfdc
    • rishabh.wason
    • sarassassin
    • araja
    • v.anand
    • lmckenzie
    • jordaneekey
    • daigo-heroku
    • mimen
    • jwadsworthsf
    • pfuente
    • montes_heroku
    • bleong
    • paguilar
    • joanneyeung
    • sbaner18
    • zli-sf
    • marcusblankenship
    • sruthich
    • mgomes-heroku
    • jesse.brown-sf
    • tij005
    • tholschuh-salesforce
    • tholmes-sfdc
    • annajohnson-sf
    • vjohne
    • slizco
    • anna-cross
    • cromwellryan-sf
    • hyunwooklee
    • matthew.rossi
    • tm-sf
    • dhawley
    • nkoziuk
    • herokucody
    • dodom-sfdc
    • csinghaus-sfdc
    • jw-sfdc
    • jlopezdan
    • gsinghsfdc
    • nrenke