Skip to content

feat(javascript): add task trigger type guards #1280

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

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void setDefaultGeneratorOptions() {
additionalProperties.put("algoliaAgent", Utils.capitalize(CLIENT));
additionalProperties.put("gitRepoId", "algoliasearch-client-javascript");
additionalProperties.put("isSearchClient", CLIENT.equals("search"));
additionalProperties.put("useAlgoliaUA", !CLIENT.equals("ingestion"));
additionalProperties.put("isIngestionClient", CLIENT.equals("ingestion"));
additionalProperties.put("isAlgoliasearchClient", isAlgoliasearchClient);

if (isAlgoliasearchClient) {
Expand Down
11 changes: 7 additions & 4 deletions templates/javascript/clients/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ export function create{{capitalizedApiName}}({
hosts: getDefaultHosts({{^hasRegionalHost}}appIdOption{{/hasRegionalHost}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}),
...options,
algoliaAgent:
{{^useAlgoliaUA}}
{{#isIngestionClient}}
{...getAlgoliaAgent({
algoliaAgents,
client: '{{{algoliaAgent}}}',
version: apiClientVersion,
}), value: ''},
{{/useAlgoliaUA}}
{{#useAlgoliaUA}}
{{/isIngestionClient}}
{{^isIngestionClient}}
getAlgoliaAgent({
algoliaAgents,
client: '{{{algoliaAgent}}}',
version: apiClientVersion,
}),
{{/useAlgoliaUA}}
{{/isIngestionClient}}
baseHeaders: {
'content-type': 'text/plain',
...auth.headers(),
Expand Down Expand Up @@ -83,6 +83,9 @@ export function create{{capitalizedApiName}}({
{{#isSearchClient}}
{{> client/api/helpers}}
{{/isSearchClient}}
{{#isIngestionClient}}
{{> client/api/guards}}
{{/isIngestionClient}}
{{#operation}}
{{> client/api/operation/jsdoc}}
{{nickname}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}( {{> client/api/operation/parameters}} ) : Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}> {
Expand Down
37 changes: 37 additions & 0 deletions templates/javascript/clients/client/api/guards.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{#isIngestionClient}}
/**
* Guard: Return strongly typed specific OnDemandTrigger for a given Trigger.
*
* @summary Guard method that returns a strongly typed specific OnDemandTrigger for a given Trigger.
* @param trigger - The given Task Trigger.
*/
isOnDemandTrigger(
trigger: Trigger
): trigger is OnDemandTrigger {
return trigger.type === 'on_demand';
},

/**
* Guard: Return strongly typed specific ScheduleTrigger for a given Trigger.
*
* @summary Guard method that returns a strongly typed specific ScheduleTrigger for a given Trigger.
* @param trigger - The given Task Trigger.
*/
isScheduleTrigger(
trigger: Trigger
): trigger is ScheduleTrigger {
return trigger.type === 'schedule';
},

/**
* Guard: Return strongly typed specific SubscriptionTrigger for a given Trigger.
*
* @summary Guard method that returns a strongly typed specific SubscriptionTrigger for a given Trigger.
* @param trigger - The given Task Trigger.
*/
isSubscriptionTrigger(
trigger: Trigger
): trigger is SubscriptionTrigger {
return trigger.type === 'subscription';
},
{{/isIngestionClient}}
7 changes: 7 additions & 0 deletions templates/javascript/clients/client/api/imports.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ import type {
{{/operation}}
} from '../model/clientMethodProps';
{{/operations}}

{{#isIngestionClient}}
import type { Trigger } from '../model/trigger';
import type { OnDemandTrigger } from '../model/onDemandTrigger';
import type { ScheduleTrigger } from '../model/scheduleTrigger';
import type { SubscriptionTrigger } from '../model/subscriptionTrigger';
{{/isIngestionClient}}