Skip to content

Commit fbc6674

Browse files
authored
feat(javascript): provide lite package (#354)
1 parent 3bdcbcc commit fbc6674

File tree

12 files changed

+203
-44
lines changed

12 files changed

+203
-44
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.2.1.0.1
1+
8.0.2

.github/actions/cache/action.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ runs:
132132
'specs/common/**'
133133
)}}
134134
135+
- name: Restore built algoliasearch-lite spec
136+
if: ${{ inputs.job == 'cts' || inputs.job == 'client' || inputs.job == 'codegen' }}
137+
uses: actions/cache@v2
138+
with:
139+
path: specs/bundled/algoliasearch-lite.yml
140+
key: |
141+
${{ env.CACHE_VERSION }}-${{
142+
hashFiles(
143+
'specs/search/**',
144+
'specs/common/**'
145+
)}}
146+
135147
- name: Restore built sources spec
136148
if: ${{ inputs.job == 'cts' || inputs.job == 'client' || inputs.job == 'codegen' }}
137149
uses: actions/cache@v2
@@ -193,6 +205,23 @@ runs:
193205
'clients/algoliasearch-client-javascript/packages/client-personalization/**'
194206
)}}
195207
208+
- name: Restore built JavaScript algoliasearch-lite client
209+
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
210+
uses: actions/cache@v2
211+
with:
212+
path: clients/algoliasearch-client-javascript/packages/algoliasearch-lite
213+
key: |
214+
${{ env.CACHE_VERSION }}-${{
215+
hashFiles(
216+
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/src/**',
217+
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/model/**',
218+
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/builds/**',
219+
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/package.json',
220+
'specs/bundled/algoliasearch-lite.yml',
221+
'templates/javascript/**',
222+
'generators/src/**'
223+
)}}
224+
196225
- name: Restore built JavaScript search client
197226
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
198227
uses: actions/cache@v2

.github/workflows/check.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,21 @@ jobs:
9494
- name: Restore cache
9595
uses: ./.github/actions/cache
9696

97-
- name: Cache '${{ matrix.client }}' bundled specs
97+
- name: Cache '${{ matrix.client.name }}' bundled specs
9898
id: cache
9999
uses: actions/cache@v2
100100
with:
101-
path: specs/bundled/${{ matrix.client }}.yml
101+
path: ${{ matrix.client.bundledPath }}
102102
key: |
103103
${{ env.CACHE_VERSION }}-${{
104104
hashFiles(
105-
format('specs/{0}/**', matrix.client),
105+
format('{0}/**', matrix.client.path),
106106
'specs/common/**'
107107
)}}
108108
109-
- name: Building '${{ matrix.client }}' specs
109+
- name: Building '${{ matrix.client.name }}' specs
110110
if: steps.cache.outputs.cache-hit != 'true'
111-
run: yarn cli build specs ${{ matrix.client }}
111+
run: yarn cli build specs ${{ matrix.client.name }}
112112

113113
client_javascript_common:
114114
timeout-minutes: 10
@@ -169,14 +169,14 @@ jobs:
169169
id: cache
170170
uses: actions/cache@v2
171171
with:
172-
path: ${{ matrix.client.folder }}
172+
path: ${{ matrix.client.path }}
173173
key: |
174174
${{ env.CACHE_VERSION }}-${{
175175
hashFiles(
176-
format('{0}/src/**', matrix.client.folder),
177-
format('{0}/model/**', matrix.client.folder),
178-
format('{0}/builds/**', matrix.client.folder),
179-
format('{0}/package.json', matrix.client.folder),
176+
format('{0}/src/**', matrix.client.path),
177+
format('{0}/model/**', matrix.client.path),
178+
format('{0}/builds/**', matrix.client.path),
179+
format('{0}/package.json', matrix.client.path),
180180
format('specs/bundled/{0}.yml', matrix.client.name),
181181
'templates/javascript/**',
182182
'generators/src/**'
@@ -220,12 +220,12 @@ jobs:
220220
id: cache
221221
uses: actions/cache@v2
222222
with:
223-
path: ${{ matrix.client.folder }}
223+
path: ${{ matrix.client.path }}
224224
key: |
225225
${{ env.CACHE_VERSION }}-${{
226226
hashFiles(
227-
format('{0}/{1}/**', matrix.client.folder, matrix.client.name),
228-
format('{0}/model/{1}/**', matrix.client.folder, matrix.client.name),
227+
format('{0}/{1}/**', matrix.client.path, matrix.client.name),
228+
format('{0}/model/{1}/**', matrix.client.path, matrix.client.name),
229229
format('specs/bundled/{0}.yml', matrix.client.name),
230230
'templates/java/**',
231231
'generators/src/**'

clients/algoliasearch-client-javascript/bundlesize.config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
55
"maxSize": "7.05KB"
66
},
7+
{
8+
"path": "packages/algoliasearch-lite/dist/algoliasearch-lite.umd.browser.js",
9+
"maxSize": "3.80KB"
10+
},
711
{
812
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
913
"maxSize": "3.85KB"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
git_push.sh
8+
.gitignore

clients/algoliasearch-client-javascript/rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function initPackagesConfig() {
112112
}
113113

114114
return availableClients.flatMap((packageName) => {
115-
const isAlgoliasearchClient = packageName.startsWith('algoliasearch');
115+
const isAlgoliasearchClient = packageName === 'algoliasearch';
116116
const commonConfig = {
117117
package: packageName,
118118
name: isAlgoliasearchClient

openapitools.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@
2626
"utilsPackageVersion": "0.0.5"
2727
}
2828
},
29+
"javascript-algoliasearch-lite": {
30+
"generatorName": "algolia-javascript",
31+
"templateDir": "#{cwd}/templates/javascript/",
32+
"config": "#{cwd}/openapitools.json",
33+
"apiPackage": "src",
34+
"output": "#{cwd}/clients/algoliasearch-client-javascript/packages/algoliasearch-lite",
35+
"glob": "specs/bundled/algoliasearch-lite.yml",
36+
"gitHost": "algolia",
37+
"gitUserId": "algolia",
38+
"gitRepoId": "algoliasearch-client-javascript",
39+
"reservedWordsMappings": "queryParameters=queryParameters,requestOptions=requestOptions",
40+
"additionalProperties": {
41+
"modelPropertyNaming": "original",
42+
"supportsES6": true,
43+
"npmName": "@experimental-api-clients-automation/algoliasearch-lite",
44+
"buildFile": "algoliasearch-lite",
45+
"apiName": "algoliasearchLite",
46+
"capitalizedApiName": "AlgoliasearchLite",
47+
"packageVersion": "0.0.5",
48+
"packageName": "@experimental-api-clients-automation/algoliasearch-lite",
49+
"utilsPackageVersion": "0.0.5"
50+
}
51+
},
2952
"javascript-recommend": {
3053
"generatorName": "algolia-javascript",
3154
"templateDir": "#{cwd}/templates/javascript/",

scripts/buildSpecs.ts

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import { checkForCache, exists, run, toAbsolutePath } from './common';
66
import { createSpinner } from './oraLog';
77
import type { Spec } from './pre-gen/setHostsOptions';
88

9+
const ALGOLIASEARCH_LITE_OPERATIONS = [
10+
'search',
11+
'multipleQueries',
12+
'searchForFacetValues',
13+
'post',
14+
];
15+
916
async function propagateTagsToOperations(
10-
bundledPath: string
17+
bundledPath: string,
18+
client: string
1119
): Promise<boolean> {
1220
if (!(await exists(bundledPath))) {
1321
throw new Error(`Bundled file not found ${bundledPath}.`);
@@ -17,17 +25,9 @@ async function propagateTagsToOperations(
1725
await fsp.readFile(bundledPath, 'utf8')
1826
) as Spec;
1927

20-
if (bundledSpec.tags.length === 0) {
21-
throw new Error(
22-
`No tags defined for ${bundledPath}, tags are required to properly generate a client.`
23-
);
24-
}
25-
26-
const tagsName = bundledSpec.tags.map((tag) => tag.name);
27-
2828
for (const pathMethods of Object.values(bundledSpec.paths)) {
2929
for (const specMethod of Object.values(pathMethods)) {
30-
specMethod.tags = tagsName;
30+
specMethod.tags = [client];
3131
}
3232
}
3333

@@ -74,22 +74,77 @@ async function lintCommon(verbose: boolean, useCache: boolean): Promise<void> {
7474
spinner.succeed();
7575
}
7676

77+
/**
78+
* Creates a lite search spec with the `ALGOLIASEARCH_LITE_OPERATIONS` methods
79+
* from the `search` spec.
80+
*/
81+
async function buildLiteSpec(
82+
spec: string,
83+
bundledPath: string,
84+
outputFormat: string,
85+
verbose: boolean
86+
): Promise<void> {
87+
const searchSpec = yaml.load(
88+
await fsp.readFile(toAbsolutePath(bundledPath), 'utf8')
89+
) as Spec;
90+
91+
searchSpec.paths = Object.entries(searchSpec.paths).reduce(
92+
(acc, [path, operations]) => {
93+
for (const [method, operation] of Object.entries(operations)) {
94+
if (
95+
method === 'post' &&
96+
ALGOLIASEARCH_LITE_OPERATIONS.includes(operation.operationId)
97+
) {
98+
return { ...acc, [path]: { post: operation } };
99+
}
100+
}
101+
102+
return acc;
103+
},
104+
{} as Spec['paths']
105+
);
106+
107+
const liteBundledPath = `specs/bundled/${spec}.${outputFormat}`;
108+
await fsp.writeFile(toAbsolutePath(liteBundledPath), yaml.dump(searchSpec));
109+
110+
if (
111+
!(await propagateTagsToOperations(toAbsolutePath(liteBundledPath), spec))
112+
) {
113+
throw new Error(
114+
`Unable to propage tags to operations for \`${spec}\` spec.`
115+
);
116+
}
117+
118+
await run(`yarn specs:fix bundled/${spec}.${outputFormat}`, {
119+
verbose,
120+
});
121+
}
122+
77123
async function buildSpec(
78-
client: string,
124+
spec: string,
79125
outputFormat: string,
80126
verbose: boolean,
81127
useCache: boolean
82128
): Promise<void> {
83-
createSpinner(`'${client}' spec`, verbose).start().info();
129+
const shouldBundleLiteSpec = spec === 'algoliasearch-lite';
130+
const client = shouldBundleLiteSpec ? 'search' : spec;
84131
const cacheFile = toAbsolutePath(`specs/dist/${client}.cache`);
85132
let hash = '';
86133

134+
createSpinner(`'${client}' spec`, verbose).start().info();
135+
87136
if (useCache) {
137+
const generatedFiles = [`bundled/${client}.yml`];
138+
139+
if (shouldBundleLiteSpec) {
140+
generatedFiles.push(`bundled/${spec}.yml`);
141+
}
142+
88143
const { cacheExists, hash: newCache } = await checkForCache(
89144
{
90145
job: `'${client}' specs`,
91146
folder: toAbsolutePath('specs/'),
92-
generatedFiles: [`bundled/${client}.yml`],
147+
generatedFiles,
93148
filesToCache: [client, 'common'],
94149
cacheFile,
95150
},
@@ -110,9 +165,7 @@ async function buildSpec(
110165
{ verbose }
111166
);
112167

113-
if (
114-
(await propagateTagsToOperations(toAbsolutePath(bundledPath))) === false
115-
) {
168+
if (!(await propagateTagsToOperations(toAbsolutePath(bundledPath), client))) {
116169
spinner.fail();
117170
throw new Error(
118171
`Unable to propage tags to operations for \`${client}\` spec.`
@@ -130,6 +183,11 @@ async function buildSpec(
130183
spinner.text = `linting '${client}' bundled spec`;
131184
await run(`yarn specs:fix bundled/${client}.${outputFormat}`, { verbose });
132185

186+
if (shouldBundleLiteSpec) {
187+
spinner.text = `Building and linting '${spec}' spec`;
188+
await buildLiteSpec(spec, bundledPath, outputFormat, verbose);
189+
}
190+
133191
if (hash) {
134192
spinner.text = `storing ${client} spec cache`;
135193
await fsp.writeFile(cacheFile, hash);

scripts/ci/createMatrix.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ type CreateMatrix = {
1010
language?: Language;
1111
};
1212

13-
type ClientMatrix = {
13+
type BaseMatrix = {
1414
name: string;
15-
folder: string;
15+
path: string;
16+
};
17+
18+
type ClientMatrix = BaseMatrix & {
1619
config?: string;
1720
api?: string;
1821
capitalizedName?: string;
1922
};
2023

24+
type SpecMatrix = BaseMatrix & {
25+
bundledPath: string;
26+
};
27+
2128
type Matrix<TMatrix> = {
2229
client: TMatrix[];
2330
};
@@ -61,7 +68,7 @@ async function getClientMatrix({
6168

6269
const matchedGenerator: ClientMatrix = {
6370
name: client,
64-
folder: output,
71+
path: output,
6572
};
6673

6774
// Extra informations for the PHP matrix in order to properly scope the
@@ -84,8 +91,8 @@ async function getClientMatrix({
8491
async function getSpecMatrix({
8592
baseBranch,
8693
baseChanged,
87-
}: CreateMatrix): Promise<Matrix<string>> {
88-
const matrix: Matrix<string> = { client: [] };
94+
}: CreateMatrix): Promise<Matrix<SpecMatrix>> {
95+
const matrix: Matrix<SpecMatrix> = { client: [] };
8996

9097
for (const client of CLIENTS) {
9198
const specChanges = await getNbGitDiff({
@@ -97,7 +104,23 @@ async function getSpecMatrix({
97104
continue;
98105
}
99106

100-
matrix.client.push(client);
107+
const spec = {
108+
name: client,
109+
path: `specs/${client}`,
110+
bundledPath: `specs/bundled/${client}.yml`,
111+
};
112+
113+
// The `algoliasearch-lite` spec is created by the `search` spec
114+
if (client === 'algoliasearch-lite') {
115+
matrix.client.push({
116+
...spec,
117+
path: 'specs/search',
118+
});
119+
120+
continue;
121+
}
122+
123+
matrix.client.push(spec);
101124
}
102125

103126
return matrix;

0 commit comments

Comments
 (0)