Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load config as a JS file only. Allow dynamic logic/options #34

Open
ballercat opened this issue Aug 4, 2023 · 4 comments
Open

Load config as a JS file only. Allow dynamic logic/options #34

ballercat opened this issue Aug 4, 2023 · 4 comments

Comments

@ballercat
Copy link
Owner

Fully switch over to using the config as a loaded js module. Add some dynamic option support, the two main features are listed below.

cache.hashFunction(request, defaultHashFunction) -> Promise<hash>

Let the user define a custom hash function. Very useful for certain type of requests. The resulting hash is used throughout for identifying the cached record (like filenames, ids). A user could for example always return the same hash value if desired for certain type of requests which maintain dynamic parameters but don't influence the response. A custom hash value can also make artifacts easily identifiable.

The hash function will always be preferred over defaultHashFunction.

stub: (request) -> Promise<response>

Let the user define custom stubs via a function. The glob matching might be too difficult.

@hamlim
Copy link
Collaborator

hamlim commented Aug 7, 2023

Do you think the stub change should be per-pattern also, or only either a function or a record?

e.g.

// jambox config
module.exports = {
  stub: {
    // "dynamic" stub:
    '**/log': (request) => {
      return Promise.resolve({ ok: true });
    },
    // "static" stub:
    '**/*.png': {
      status: 200,
      preferNetwork: true,
      file: path.join(__dirname, '.jambox', 'placeholder.png'),
    },
  }
}

Or:

// jambox config
module.exports = {
  stub: request => {
    if (matches(request.url, '**/log')) {
      return Promise.resolve({ ok: true });
    } else if (matches(request.url, '**/.png')) {
      return {
        status: 200,
        preferNetwork: true,
        file: path.join(__dirname, '.jambox', 'placeholder.png'),
      };
    }
    return Promise.reject({notStubbed: true});
  }
}

@ballercat
Copy link
Owner Author

@hamlim

Strong preference for the latter. Though to be fair, both versions could be allowed. One function for all stubs supports every possible scenario so it would at least make sense as the first take.

@hamlim
Copy link
Collaborator

hamlim commented Aug 7, 2023

Would it be worth exposing a "matches" like function also to the function (similar to defaultHashFunction, that does the same matching logic that already happens today?

example:

module.exports = {
  stub(request, { defaultMatch }) {
    if (defaultMatch(request.url, '**/log')) {
      //
    }
  }
}

@ballercat
Copy link
Owner Author

Yeah sure that can be done. Is it worth it? I don't know, it's not difficult to provide it, but its also might not end-up being very useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants