├── .gitignore
├── .npmignore
├── CHANGELOG.md
├── examples
├── fetch.res
├── middleware.res
├── cat-fact
│ ├── index.html
│ └── App.res
├── error.res
└── provider.res
├── prepublish.sh
├── rescript.json
├── src
├── SwrMutation.res
├── SwrInfinite.res
├── SwrCommon.res
├── Swr.res
└── SwrEasy.res
├── LICENSE
├── package.json
├── README.md
└── pnpm-lock.yaml
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .vscode
3 | .merlin
4 | .bsb.lock
5 | npm-debug.log
6 | /lib/bs/
7 | /lib/ocaml/
8 | /node_modules/
9 | debug.log
10 | *.cmi
11 | *.cmj
12 | *.cmt
13 | *.bs.js
14 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .vscode
3 | .merlin
4 | .bsb.lock
5 | npm-debug.log
6 | /lib/bs/
7 | /lib/ocaml/
8 | /node_modules/
9 | debug.log
10 | *.cmi
11 | *.cmj
12 | *.cmt
13 | *.bs.js
14 | /examples/
15 |
16 | prepublish.sh
17 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4 |
5 | ## 3.0.0-beta.1 (2022-09-10)
6 |
--------------------------------------------------------------------------------
/examples/fetch.res:
--------------------------------------------------------------------------------
1 | exception FetchError(string, int)
2 |
3 | let fetcher = (url) => {
4 | Promise.make((resolve, reject)=>{
5 | if (url === "") {
6 | reject(FetchError("Url is empty!", 400))
7 | }
8 | else {
9 | resolve("Got data!")
10 | }
11 | })
12 | }
13 |
--------------------------------------------------------------------------------
/examples/middleware.res:
--------------------------------------------------------------------------------
1 | /*
2 | Examples adapted from https://swr.vercel.app/docs/middleware
3 | */
4 |
5 | open Swr
6 |
7 | let logger = useSWRNext => (key, fetcher, config) => {
8 | let extendedFetcher = args => {
9 | Console.log2("SWR Request: ", key)
10 | fetcher(args)
11 | }
12 | useSWRNext(key, extendedFetcher, config)
13 | }
14 |
15 | let swr = useSWR_config(
16 | "key",
17 | Fetch.fetcher,
18 | {
19 | use: [logger],
20 | },
21 | )
22 | Console.log(swr.data)
23 |
--------------------------------------------------------------------------------
/prepublish.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | NPM_VERSION=$(jq -r ".version" package.json)
4 | BS_VERSION=$(jq -r ".version" rescript.json)
5 |
6 | SWR_VERSION=$(jq ".dependencies.swr" package.json)
7 |
8 | if [ "$NPM_VERSION" != "$BS_VERSION" ]; then
9 | echo "Versions do not match. Exiting..."
10 | exit 1
11 | fi
12 |
13 | read -p "Is SWR version ${SWR_VERSION} corresponding to package \
14 | version ${NPM_VERSION} correct? (y/n): " -n 1 -r
15 | echo
16 | if [[ $REPLY =~ ^[Nn]$ ]]; then
17 | echo "Aborted publishing process."
18 | exit 1
19 | fi
20 |
--------------------------------------------------------------------------------
/examples/cat-fact/index.html:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Cat Fact
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/rescript.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "rescript-swr",
3 | "version": "3.0.0-beta.4",
4 | "sources": [
5 | {
6 | "dir": "examples",
7 | "subdirs": true,
8 | "type": "dev"
9 | },
10 | {
11 | "dir": "src",
12 | "subdirs": true
13 | }
14 | ],
15 | "package-specs": {
16 | "module": "esmodule",
17 | "in-source": true
18 | },
19 | "suffix": ".bs.js",
20 | "jsx": {
21 | "version": 4
22 | },
23 | "bs-dependencies": [
24 | "@rescript/core",
25 | "@rescript/react"
26 | ],
27 | "bsc-flags": [
28 | "-open RescriptCore"
29 | ],
30 | "warnings": {
31 | "error": "+101"
32 | },
33 | "namespace": false
34 | }
35 |
--------------------------------------------------------------------------------
/src/SwrMutation.res:
--------------------------------------------------------------------------------
1 | type fetcherOpts<'arg> = {arg: 'arg}
2 | type fetcher<'key, 'arg, 'data> = ('key, fetcherOpts<'arg>) => promise<'data>
3 |
4 | type rec swrMutationConfig<'key, 'arg, 'data> = {
5 | fetcher?: fetcher<'key, 'arg, 'data>,
6 | onError?: (Exn.t, 'key, swrMutationConfig<'key, 'arg, 'data>) => unit,
7 | onSuccess?: ('data, 'key, swrMutationConfig<'key, 'arg, 'data>) => unit,
8 | revalidate?: bool,
9 | populateCache?: (Obj.t, 'data) => 'data,
10 | optimisticData?: 'data => 'data,
11 | rollbackOnError?: Obj.t => bool,
12 | throwOnError?: bool,
13 | }
14 |
15 | type swrMutationResponse<'key, 'arg, 'data> = {
16 | data: 'data,
17 | error: Exn.t,
18 | isMutating: bool,
19 | reset: unit => unit,
20 | trigger: ('arg, ~config: swrMutationConfig<'key, 'arg, 'data>=?) => promise<'data>,
21 | }
22 |
23 | @val @module("swr/mutation")
24 | external useSWRMutation: (
25 | 'key,
26 | fetcher<'key, 'arg, 'data>,
27 | ~config: swrMutationConfig<'key, 'arg, 'data>=?,
28 | ) => swrMutationResponse<'key, 'arg, 'data> = "default"
29 |
--------------------------------------------------------------------------------
/src/SwrInfinite.res:
--------------------------------------------------------------------------------
1 | open SwrCommon
2 |
3 | type keyLoader<'key, 'any> = (int, option<'any>) => option<'key>
4 |
5 | type swrInfiniteResponse<'data> = {
6 | ...swrResponse<'data>,
7 | size: int,
8 | setSize: (int => int) => promise