Skip to content

Configuration Reference

The following section contains references to all configuration options in the provided libraries.

Code Generation

In order to customize code generation, you can add an initializer to your Rails app:

# config/initializers/js_from_routes.rb
if Rails.env.development?
  JsFromRoutes.config do |config|
    ...
  end
end

You can fully customize how code is generated, which client libraries to use, whether to generate TypeScript, target jQuery, or adapt to your framework of choice.

The following config options are available:

all_helpers_file

Whether to generate a file that exports all available helpers.

You can specify a different name for the file, or pass false to disable it.

Default: true, will output index.js in the output_folder

config.all_helpers_file = false # Don't generate the file

client_library

The library from which to import definePathHelper in the default template.

Read more about it in Code Generation.

Default: @js-from-routes/client

config.client_library = '~/services/ApiService'

export_if

Allows to configure which routes should be exported.

Enables advanced usages, such as exporting different routes to different paths, which is helpful for monoliths with several apps inside them.

Default: ->(route) { route.defaults.fetch(:export, nil) }

config.export_if = ->(route) { route.defaults[:export] == :main }

file_suffix

This suffix is added by default to all generated files. You can modify it if you are using TypeScript, or want to use a different convention.

Default: Api.js

config.file_suffix = 'Api.ts'

helper_mappings

Defines how to obtain a path helper name from the name of a route (controller action).

Default: {"index" => "list", "show" => "get"}

config.helper_mappings = {"edit" => "infoForUpdate"}

output_folder

The directory where the generated files are created.

By default it will use the first of the following directories that exists.

Default: app/{frontend,javascript,packs,assets}/api

config.output_folder = Rails.root.join('app', 'path_helpers')

template_path

The path of an ERB template that will be used to generate a helpers file.

Read more about it in Code Generation.

Default

config.template_path = Rails.root.join('custom_js_from_routes.js.erb')

template_all_path

The path of an ERB template that will be used to export all helper files.

You can provide a path to a custom template if the default conventions don't suit your needs.

Default

config.template_all_path = Rails.root.join('custom_all_helpers.js.erb')

template_index_path

Similar to the above, it re-exports all helpers, but also combines them in a default export allowing you to use a single object to access all the helpers.

You can provide a path to a custom template if the default conventions don't suit your needs.

Default

config.template_index_path = Rails.root.join('custom_index_helpers.js.erb')

Client Configuration

Coming soon...

For now, check the Client Libraries section.




















Configuration Reference has loaded