Skip to content

Commit ef684de

Browse files
authored
chore: multi line snippets and docs feedbacks (#3770)
1 parent 557136d commit ef684de

File tree

18 files changed

+273
-34
lines changed

18 files changed

+273
-34
lines changed

config/generation.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const patterns = [
55

66
'clients/**',
77
'snippets/**',
8+
'snippets/guides/**',
89
'!clients/README.md',
910
'!clients/**/.openapi-generator-ignore',
1011
'clients/**/.github/**',
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { transformCodeSamplesToGuideMethods } from '../snippets';
4+
import { SnippetSamples } from '../types';
5+
6+
describe('init', () => {
7+
it('parses a multi line import', () => {
8+
expect(
9+
JSON.parse(
10+
transformCodeSamplesToGuideMethods({
11+
csharp: {
12+
foo: {
13+
default: `
14+
// Initialize the client
15+
var client = new QuerySuggestionsClient(
16+
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
17+
);
18+
19+
// Call the API
20+
var response = await client.CreateConfigAsync(
21+
new ConfigurationWithIndex
22+
{
23+
IndexName = "<YOUR_INDEX_NAME>",
24+
SourceIndices = new List<SourceIndex>
25+
{
26+
new SourceIndex
27+
{
28+
IndexName = "<YOUR_INDEX_NAME>",
29+
Facets = new List<Facet> { new Facet { Attribute = "test" } },
30+
Generate = new List<List<string>>
31+
{
32+
new List<string> { "facetA", "facetB" },
33+
new List<string> { "facetC" },
34+
},
35+
},
36+
},
37+
Languages = new Languages(new List<string> { "french" }),
38+
Exclude = new List<string> { "test" },
39+
}
40+
);
41+
// >LOG
42+
`,
43+
},
44+
},
45+
} as unknown as SnippetSamples),
46+
),
47+
).toMatchInlineSnapshot(`
48+
{
49+
"csharp": {
50+
"foo": {
51+
"default": "var response = await client.CreateConfigAsync(
52+
new ConfigurationWithIndex
53+
{
54+
IndexName = "<YOUR_INDEX_NAME>",
55+
SourceIndices = new List<SourceIndex>
56+
{
57+
new SourceIndex
58+
{
59+
IndexName = "<YOUR_INDEX_NAME>",
60+
Facets = new List<Facet> { new Facet { Attribute = "test" } },
61+
Generate = new List<List<string>>
62+
{
63+
new List<string> { "facetA", "facetB" },
64+
new List<string> { "facetC" },
65+
},
66+
},
67+
},
68+
Languages = new Languages(new List<string> { "french" }),
69+
Exclude = new List<string> { "test" },
70+
}
71+
);",
72+
},
73+
"init": {
74+
"default": "var client = new QuerySuggestionsClient(
75+
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
76+
);",
77+
},
78+
},
79+
}
80+
`);
81+
});
82+
83+
it('parses a single line import', () => {
84+
expect(
85+
JSON.parse(
86+
transformCodeSamplesToGuideMethods({
87+
csharp: {
88+
foo: {
89+
default: `
90+
// Initialize the client
91+
var client = new QuerySuggestionsClient(new Client("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION"));
92+
93+
// Call the API
94+
var response = await client.CreateConfigAsync(
95+
new ConfigurationWithIndex
96+
{
97+
IndexName = "<YOUR_INDEX_NAME>",
98+
SourceIndices = new List<SourceIndex>
99+
{
100+
new SourceIndex
101+
{
102+
IndexName = "<YOUR_INDEX_NAME>",
103+
Facets = new List<Facet> { new Facet { Attribute = "test" } },
104+
Generate = new List<List<string>>
105+
{
106+
new List<string> { "facetA", "facetB" },
107+
new List<string> { "facetC" },
108+
},
109+
},
110+
},
111+
Languages = new Languages(new List<string> { "french" }),
112+
Exclude = new List<string> { "test" },
113+
}
114+
);
115+
// >LOG
116+
`,
117+
},
118+
},
119+
} as unknown as SnippetSamples),
120+
),
121+
).toMatchInlineSnapshot(`
122+
{
123+
"csharp": {
124+
"foo": {
125+
"default": "var response = await client.CreateConfigAsync(
126+
new ConfigurationWithIndex
127+
{
128+
IndexName = "<YOUR_INDEX_NAME>",
129+
SourceIndices = new List<SourceIndex>
130+
{
131+
new SourceIndex
132+
{
133+
IndexName = "<YOUR_INDEX_NAME>",
134+
Facets = new List<Facet> { new Facet { Attribute = "test" } },
135+
Generate = new List<List<string>>
136+
{
137+
new List<string> { "facetA", "facetB" },
138+
new List<string> { "facetC" },
139+
},
140+
},
141+
},
142+
Languages = new Languages(new List<string> { "french" }),
143+
Exclude = new List<string> { "test" },
144+
}
145+
);",
146+
},
147+
"init": {
148+
"default": "var client = new QuerySuggestionsClient(new Client("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION"));",
149+
},
150+
},
151+
}
152+
`);
153+
});
154+
});
155+
156+
describe('initialize', () => {
157+
it("doesn't stop at `client`", () => {
158+
expect(
159+
JSON.parse(
160+
transformCodeSamplesToGuideMethods({
161+
csharp: {
162+
foo: {
163+
default: `
164+
// Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ
165+
var client = new QuerySuggestionsClient(
166+
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
167+
);
168+
169+
// Call the API
170+
var response = await client.CreateConfigAsync(
171+
new ConfigurationWithIndex
172+
{
173+
IndexName = "<YOUR_INDEX_NAME>",
174+
SourceIndices = new List<SourceIndex>
175+
{
176+
new SourceIndex
177+
{
178+
IndexName = "<YOUR_INDEX_NAME>",
179+
Facets = new List<Facet> { new Facet { Attribute = "test" } },
180+
Generate = new List<List<string>>
181+
{
182+
new List<string> { "facetA", "facetB" },
183+
new List<string> { "facetC" },
184+
},
185+
},
186+
},
187+
Languages = new Languages(new List<string> { "french" }),
188+
Exclude = new List<string> { "test" },
189+
}
190+
);
191+
// >LOG
192+
`,
193+
},
194+
},
195+
} as unknown as SnippetSamples),
196+
),
197+
).toMatchInlineSnapshot(`
198+
{
199+
"csharp": {
200+
"foo": {
201+
"default": "var response = await client.CreateConfigAsync(
202+
new ConfigurationWithIndex
203+
{
204+
IndexName = "<YOUR_INDEX_NAME>",
205+
SourceIndices = new List<SourceIndex>
206+
{
207+
new SourceIndex
208+
{
209+
IndexName = "<YOUR_INDEX_NAME>",
210+
Facets = new List<Facet> { new Facet { Attribute = "test" } },
211+
Generate = new List<List<string>>
212+
{
213+
new List<string> { "facetA", "facetB" },
214+
new List<string> { "facetC" },
215+
},
216+
},
217+
},
218+
Languages = new Languages(new List<string> { "french" }),
219+
Exclude = new List<string> { "test" },
220+
}
221+
);",
222+
},
223+
"init": {
224+
"default": "var client = new QuerySuggestionsClient(
225+
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
226+
);",
227+
},
228+
},
229+
}
230+
`);
231+
});
232+
});

scripts/specs/snippets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export function transformCodeSamplesToGuideMethods(snippetSamples: SnippetSample
2828

2929
for (const [sampleName, sample] of Object.entries(samples)) {
3030
const sampleMatch = sample.match(
31-
/.*Initialize the client.*\n(.*)((.|\n)*)(.*Call the API\n)((.|\n)*)(#|\/\/) >LOG/,
31+
/.*Initialize the client.*([\s\S]*?)(#|\/\/) Call the API([\s\S]*?)(#|\/\/) >LOG/,
3232
);
3333
if (!sampleMatch) {
3434
continue;
3535
}
3636

3737
const initLine = sampleMatch[1];
38-
const callLine = sampleMatch[5];
38+
const callLine = sampleMatch[3];
3939

4040
if (!('init' in snippetSamples[language])) {
4141
snippetSamples[language].init = {

templates/csharp/snippets/method.mustache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// {{generationBanner}}
2-
using Algolia.Search.Http;
32
// >IMPORT
43
using Algolia.Search.Clients;
5-
// IMPORT<
64
using Algolia.Search.Models.{{clientPrefix}};
5+
// IMPORT<
76
{{#isSearchClient}}
87
using Action = Algolia.Search.Models.Search.Action;
98
{{/isSearchClient}}
@@ -25,7 +24,7 @@ public class Snippet{{client}}
2524
{
2625
// >SEPARATOR {{method}} {{testName}}
2726
// Initialize the client
28-
var client = new {{client}}(new {{clientPrefix}}Config("YOUR_APP_ID", "YOUR_API_KEY"{{#hasRegionalHost}},"YOUR_APP_ID_REGION"{{/hasRegionalHost}}));
27+
var client = new {{client}}(new {{clientPrefix}}Config("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"{{#hasRegionalHost}},"ALGOLIA_APPLICATION_REGION"{{/hasRegionalHost}}));
2928

3029
// Call the API
3130
{{#hasResponse}}var response = {{/hasResponse}}{{> tests/method}};
@@ -35,4 +34,4 @@ public class Snippet{{client}}
3534

3635
{{/snippets}}
3736
{{/blocksRequests}}
38-
}
37+
}

templates/dart/snippets/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import '{{{import}}}';
1111
void snippetFor{{method}}{{testIndex}}() async {
1212
// >SEPARATOR {{method}} {{testName}}
1313
// Initialize the client
14-
final client = {{client}}(appId: 'YOUR_APP_ID', apiKey: 'YOUR_API_KEY'{{#hasRegionalHost}}, region: 'YOUR_APP_ID_REGION'{{/hasRegionalHost}});
14+
final client = {{client}}(appId: 'ALGOLIA_APPLICATION_ID', apiKey: 'ALGOLIA_API_KEY'{{#hasRegionalHost}}, region: 'ALGOLIA_APPLICATION_REGION'{{/hasRegionalHost}});
1515

1616
// Call the API
1717
{{#hasResponse}}final response = {{/hasResponse}}{{#isAsyncMethod}}await {{/isAsyncMethod}}client.{{method}}(

templates/go/snippets/method.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func SnippetFor{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Of{{#lambda.p
1616
*/
1717

1818
// >SEPARATOR {{method}} {{testName}}
19-
// Initialize the client{{#hasRegionalHost}} with your application region, eg. {{clientPrefix}}.YOUR_APP_ID_REGION{{/hasRegionalHost}}
20-
client, err := {{clientPrefix}}.NewClient("YOUR_APP_ID", "YOUR_API_KEY"{{#hasRegionalHost}}, {{clientPrefix}}.{{#lambda.uppercase}}{{defaultRegion}}{{/lambda.uppercase}}{{/hasRegionalHost}})
19+
// Initialize the client{{#hasRegionalHost}} with your application region, eg. {{clientPrefix}}.ALGOLIA_APPLICATION_REGION{{/hasRegionalHost}}
20+
client, err := {{clientPrefix}}.NewClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"{{#hasRegionalHost}}, {{clientPrefix}}.{{#lambda.uppercase}}{{defaultRegion}}{{/lambda.uppercase}}{{/hasRegionalHost}})
2121
if err != nil {
2222
// The client can fail to initialize if you pass an invalid parameter.
2323
panic(err)
@@ -38,4 +38,4 @@ func SnippetFor{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Of{{#lambda.p
3838
// SEPARATOR<
3939
}
4040
{{/snippets}}
41-
{{/blocksRequests}}
41+
{{/blocksRequests}}

templates/java/snippets/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Snippet{{client}} {
1515
void snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}{{testIndex}}() {
1616
// >SEPARATOR {{method}} {{testName}}
1717
// Initialize the client
18-
{{client}} client = new {{client}}("YOUR_APP_ID", "YOUR_API_KEY"{{#hasRegionalHost}}, "YOUR_APP_ID_REGION"{{/hasRegionalHost}});
18+
{{client}} client = new {{client}}("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"{{#hasRegionalHost}}, "ALGOLIA_APPLICATION_REGION"{{/hasRegionalHost}});
1919

2020
// Call the API
2121
{{> tests/method}};

templates/javascript/snippets/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { RequestOptions } from '@algolia/client-common';
1414
export {{#isAsyncMethod}}async{{/isAsyncMethod}} function snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}{{testIndex}}(): {{#isAsyncMethod}}Promise<void>{{/isAsyncMethod}}{{^isAsyncMethod}}void{{/isAsyncMethod}} {
1515
// >SEPARATOR {{method}} {{testName}}
1616
// Initialize the client
17-
const client = {{client}}("YOUR_APP_ID", "YOUR_API_KEY", {{#hasRegionalHost}}'YOUR_APP_ID_REGION', {{/hasRegionalHost}});
17+
const client = {{client}}("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", {{#hasRegionalHost}}'ALGOLIA_APPLICATION_REGION', {{/hasRegionalHost}});
1818

1919
// Call the API
2020
{{#hasResponse}}const response = {{/hasResponse}}{{> tests/method}};

templates/kotlin/snippets/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Snippet{{client}} {
1717
suspend fun snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}{{testIndex}}() {
1818
// >SEPARATOR {{method}} {{testName}}
1919
// Initialize the client
20-
val client = {{client}}(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY"{{#hasRegionalHost}}, region = "YOUR_APP_ID_REGION"{{/hasRegionalHost}})
20+
val client = {{client}}(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY"{{#hasRegionalHost}}, region = "ALGOLIA_APPLICATION_REGION"{{/hasRegionalHost}})
2121

2222
// Call the API
2323
{{#hasResponse}}var response = {{/hasResponse}}client.{{> tests/method}}

templates/php/snippets/method.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Snippet{{client}}
2121
{
2222
// >SEPARATOR {{method}} {{testName}}
2323
// Initialize the client
24-
$client = {{client}}::create('<YOUR_APP_ID>', '<YOUR_API_KEY>'{{#hasRegionalHost}}, 'YOUR_APP_ID_REGION'{{/hasRegionalHost}});
24+
$client = {{client}}::create('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY'{{#hasRegionalHost}}, 'ALGOLIA_APPLICATION_REGION'{{/hasRegionalHost}});
2525

2626
// Call the API
2727
{{#hasResponse}}$response = {{/hasResponse}}{{> tests/method}};
@@ -36,4 +36,4 @@ class Snippet{{client}}
3636

3737
{{/snippets}}
3838
{{/blocksRequests}}
39-
}
39+
}

templates/python/snippets/method.mustache

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ async def snippet_for_{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}{{test
1313
"""
1414
# >SEPARATOR {{method}} {{testName}}
1515
# Initialize the client
16-
_client = {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}("YOUR_APP_ID", "YOUR_API_KEY"{{#hasRegionalHost}}, "YOUR_APP_ID_REGION"{{/hasRegionalHost}})
16+
client = {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"{{#hasRegionalHost}}, "ALGOLIA_APPLICATION_REGION"{{/hasRegionalHost}})
1717

18-
# Call the API
19-
{{#hasResponse}}response = {{/hasResponse}}{{#isAsyncMethod}}await {{/isAsyncMethod}}_client.{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}({{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}} request_options={ {{#requestOptions.headers.parameters}}"headers":loads("""{{{.}}}"""),{{/requestOptions.headers.parameters}}{{#requestOptions.queryParameters.parameters}}"query_parameters":loads("""{{{.}}}"""),{{/requestOptions.queryParameters.parameters}} }{{/hasRequestOptions}})
18+
# Call the API
19+
{{#hasResponse}}response = {{/hasResponse}}{{#isAsyncMethod}}await {{/isAsyncMethod}}client.{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}({{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}} request_options={ {{#requestOptions.headers.parameters}}"headers":loads("""{{{.}}}"""),{{/requestOptions.headers.parameters}}{{#requestOptions.queryParameters.parameters}}"query_parameters":loads("""{{{.}}}"""),{{/requestOptions.queryParameters.parameters}} }{{/hasRequestOptions}})
20+
21+
{{#hasResponse}}print(response){{/hasResponse}}
22+
23+
# Skip deserialization
24+
{{#hasResponse}}raw_response = {{/hasResponse}}{{#isAsyncMethod}}await {{/isAsyncMethod}}client.{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}_with_http_info({{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}} request_options={ {{#requestOptions.headers.parameters}}"headers":loads("""{{{.}}}"""),{{/requestOptions.headers.parameters}}{{#requestOptions.queryParameters.parameters}}"query_parameters":loads("""{{{.}}}"""),{{/requestOptions.queryParameters.parameters}} }{{/hasRequestOptions}})
25+
26+
{{#hasResponse}}print(raw_response.raw_data){{/hasResponse}}
2027

2128
# >LOG
2229
{{#hasResponse}}

templates/ruby/snippets/method.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require 'algolia'
1111
def snippet_for_{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}{{testIndex}}
1212
# >SEPARATOR {{method}} {{testName}}
1313
# Initialize the client
14-
client = Algolia::{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}.create('YOUR_APP_ID','YOUR_API_KEY'{{#hasRegionalHost}},'YOUR_APP_ID_REGION'{{/hasRegionalHost}})
14+
client = Algolia::{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}.create('ALGOLIA_APPLICATION_ID','ALGOLIA_API_KEY'{{#hasRegionalHost}},'ALGOLIA_APPLICATION_REGION'{{/hasRegionalHost}})
1515

1616
# Call the API
1717
{{#hasResponse}}response = {{/hasResponse}}client.{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}({{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}}{ {{#requestOptions.headers.parameters}}:header_params => JSON.parse('{{{.}}}', :symbolize_names => true),{{/requestOptions.headers.parameters}}{{#requestOptions.queryParameters.parameters}}:query_params => JSON.parse('{{{.}}}', :symbolize_names => true){{/requestOptions.queryParameters.parameters}} }{{/hasRequestOptions}})
@@ -28,4 +28,4 @@ def snippet_for_{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}{{testIndex}
2828
end
2929

3030
{{/snippets}}
31-
{{/blocksRequests}}
31+
{{/blocksRequests}}

0 commit comments

Comments
 (0)