3.0.44 • Published 15 days ago

haystack-nclient v3.0.44

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
15 days ago

Haystack Client

A network client haystack implementation written in TypeScript. This API targets standard Haystack web servers plus other non-standard implementations.

This library uses haystack-core.

Optionally use haystack-units for the unit database.

If you're building an application using React try using haystack-react in addition to the core and client libraries.

Installation

Use the following npm command to install haystack client...

npm install haystack-core haystack-units haystack-nclient

Note how haystack-core must be installed as a peer dependency.

APIs

Please click here for the API documentation.

Servers

This library is used to talk to array of different Haystack servers...

  • FIN 5.X: only use the methods under Client ops and ext.
    • Everything under ops uses all the standard Haystack Ops.
    • Everything under ext uses methods specific to SkySpark (i.e. evaluating Axon).
  • Future: in addition to ops, try using the newer Haystack enabled REST API services under Client (i.e. record, schedule, user or proj). Prefer REST API services over ops. Never use ext.

Client

A set of useful high level of APIs for working with Haystack data in the FIN framework's server.

// Query all the available sites server side using the Haystack read op...
const grid = await client.ops.read('site')

Unless you're building your own APIs, it's recommended to use the Client APIs. These APIs build on top of the fetchVal API.

Construction

Please note, since a Client instance contains state (i.e. maintains a list of watches), always cache and reuse a Client instance. Do not create a new Client instance everytime you use it!

If you're using haystack-react, the Client is created from a React Context. Use a hook called useClient() to get access to the Client.

If you need to create your own Client object...

FIN5

// Create a client object using the web browser's current URI.
// The project name will be parsed from the URI.
const client = new Client({
	base: new URL(window.location.href),
})

// Explicitly define what project to use...
const clientWithProject = new Client({
	base: new URL(window.location.href),
	project: 'demo',
})

Watch

Please note, haystack-react contains some hooks that makes this even easier!

Watches are used to watch for live events on records...

// Resolve a grid with ids.
const grid = await client.ops.read('point and navName == "SAT"')

// Create a watch and give it a display name.
const watch = await client.ops.watch.make('All SAT points', grid)

// Add event handlers. We're only interested in 'curVal' changes.
watch.changed({
	interests: ['curVal'],
	callback: (event) => console.log(event)
})

...

// Always close a watch after using it.
await watch.close()

Haystack filters can also be used to watch for specific conditions...

// Add event handlers. We're only interested in changes to curVal when it's above a certain value.
watch.changed({
	interests: ['curVal'],
	condition: 'curVal > 50°F',
	callback: (event) => console.log(event),
})

fetchVal

The fetchVal API builds on top of hsFetch. It adds all the necessary encoding and decoding of a haystack value in one API call.

const dict = await fetchVal<HDict>('/api/demo/somethingnew')

For backwards compatibility, there is also a fetchGrid that calls fetchVal.

Hayson

Decoding values from Hayson will happen transparently when the server responds with the correct MIME type.

To send Hayson, consider the following code...

const dict = await fetchVal<HDict>('/api/demo/somethingnew', {
	method: 'POST',
	headers: { 'content-type': HAYSON_MIME_TYPE },
	body: JSON.stringify(hval.toJSON()),
})

finCsrfFetch

The fetch API is used by web developers to make network calls.

The finCsrfFetch API wraps fetch and automatic background management of CSRF tokens (a.k.a. Attest Keys) for the FIN framework.

Use this API if you want to work with the web server but aren't going to work with grids.

// Call a JSON REST API...
const resp = await finCsrfFetch('/someApi')
const someJson = await resp.json()
3.0.44

15 days ago

3.0.43

2 months ago

3.0.42

3 months ago

3.0.41

4 months ago

3.0.40

9 months ago

3.0.38

10 months ago

3.0.39

9 months ago

3.0.37

12 months ago

3.0.34

1 year ago

3.0.35

12 months ago

3.0.33

1 year ago

3.0.32

1 year ago

3.0.31

1 year ago

3.0.32-beta.1

1 year ago

3.0.30

1 year ago

3.0.29

2 years ago

3.0.27

2 years ago

3.0.28

2 years ago

3.0.25

2 years ago

3.0.26

2 years ago

3.0.23

2 years ago

3.0.24

2 years ago

3.0.21

2 years ago

3.0.22

2 years ago

3.0.20

2 years ago

3.0.18

2 years ago

3.0.19

2 years ago

3.0.13

2 years ago

3.0.16

2 years ago

3.0.17

2 years ago

3.0.14

2 years ago

3.0.15

2 years ago

3.0.22-beta.1

2 years ago

3.0.22-beta.2

2 years ago

3.0.12

2 years ago

3.0.11

2 years ago

3.0.10

2 years ago

3.0.8

2 years ago

3.0.9

2 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

0.1.0-beta.0

3 years ago

3.0.0

3 years ago

2.0.11

3 years ago