Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ runs:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/packages/client-sources/dist
key: ${{ runner.os }}-1-js-client-sources-${{ hashFiles('clients/algoliasearch-client-javascript/packages/client-sources/**') }}-${{ hashFiles('specs/dist/sources.yml') }}

- name: Restore built JavaScript predict client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
with:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/packages/client-predict/dist
key: ${{ runner.os }}-1-js-client-predict-${{ hashFiles('clients/algoliasearch-client-javascript/packages/client-predict/**') }}-${{ hashFiles('specs/dist/predict.yml') }}

- name: Restore built Java client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
Expand Down
1 change: 1 addition & 0 deletions .redocly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ apiDefinitions:
recommend: specs/recommend/spec.yml
search: specs/search/spec.yml
sources: specs/sources/spec.yml
predict: specs/predict/spec.yml

lint:
extends:
Expand Down
1 change: 1 addition & 0 deletions clients/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This folder hosts the generated clients.
- [@algolia/client-search](./algoliasearch-client-javascript/packages/client-search/): The Algolia search client.
- [@algolia/recommend](./algoliasearch-client-javascript/packages/recommend/): The Algolia recommend client.
- [@algolia/sources](./algoliasearch-client-javascript/packages/client-sources/): The Algolia sources client.
- [@algolia/predict](./algoliasearch-client-javascript/packages/client-predict/): The Algolia predict client.
- [@algolia/client-common](./algoliasearch-client-javascript/packages/client-common/): The JavaScript clients common files.
- [@algolia/requester-browser-xhr](./algoliasearch-client-javascript/packages/requester-browser-xhr/): Browser XHR requester for the Algolia JavaScript clients.
- [@algolia/requester-node-http](./algoliasearch-client-javascript/packages/requester-node-http/): Node.js HTTP requester for the Algolia JavaScript clients.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

git_push.sh
model/models.ts
.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Host, Requester } from '@algolia/client-common';
import { XhrRequester } from '@algolia/requester-browser-xhr';

import { createPredictApi } from './src/predictApi';
import type { PredictApi } from './src/predictApi';

export * from './src/predictApi';

export function predictApi(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
): PredictApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

return createPredictApi({
appId,
apiKey,

timeouts: {
connect: 1,
read: 2,
write: 30,
},
requester: options?.requester ?? new XhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
...options,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Affinities = {
name?: string;
value?: string;
probability?: number;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Error.
*/
export type ErrorBase = {
message?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Predictions } from './predictions';
import type { Properties } from './properties';
import type { Segments } from './segments';

export type FetchUserProfileResponse = {
user: string;
predictions?: Predictions;
properties?: Properties;
segments?: Segments;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type FunnelStage = {
name?: string;
probability?: number;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Object with models and types to retrieve.
*/
export type Params = {
/**
* List with model types for which to retrieve predictions.
*/
modelsToRetrieve?: ParamsModelsToRetrieve[];
/**
* List with types to be retrieved.
*/
typesToRetrieve?: ParamsTypesToRetrieve[];
};

export type ParamsModelsToRetrieve =
| 'affinities'
| 'funnel_stage'
| 'order_value';

export type ParamsTypesToRetrieve = 'properties' | 'segments';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PredictionsAffinities } from './predictionsAffinities';
import type { PredictionsFunnelStage } from './predictionsFunnelStage';
import type { PredictionsOrderValue } from './predictionsOrderValue';

export type Predictions = {
funnel_stage?: PredictionsFunnelStage;
order_value?: PredictionsOrderValue;
affinities?: PredictionsAffinities;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Affinities } from './affinities';

/**
* Prediction for the **affinities** model.
*/
export type PredictionsAffinities = {
value?: Affinities[];
lastUpdatedAt?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { FunnelStage } from './funnelStage';

/**
* Prediction for the **funnel_stage** model.
*/
export type PredictionsFunnelStage = {
value?: FunnelStage[];
lastUpdatedAt?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Prediction for the **order_value** model.
*/
export type PredictionsOrderValue = {
value?: number;
lastUpdatedAt?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Properties for the user profile.
*/
export type Properties = {
/**
* Raw user properties (key-value pairs).
*/
raw?: Record<string, any>;
/**
* Computed user properties (key-value pairs).
*/
computed?: Record<string, any>;
/**
* Custom user properties (key-value pairs).
*/
custom?: Record<string, any>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Segments that the user belongs to.
*/
export type Segments = {
/**
* List of computed segments IDs.
*/
computed?: string[];
/**
* List of custom segments IDs.
*/
custom?: string[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Host, Requester } from '@algolia/client-common';
import { HttpRequester } from '@algolia/requester-node-http';

import { createPredictApi } from './src/predictApi';
import type { PredictApi } from './src/predictApi';

export * from './src/predictApi';

export function predictApi(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
): PredictApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

return createPredictApi({
appId,
apiKey,

timeouts: {
connect: 2,
read: 5,
write: 30,
},
requester: options?.requester ?? new HttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
...options,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@algolia/client-predict",
"version": "0.0.1",
"description": "JavaScript client for @algolia/client-predict",
"repository": "algolia/algoliasearch-client-javascript",
"author": "Algolia",
"private": true,
"license": "MIT",
"main": "./dist/node.js",
"types": "./dist/node.d.ts",
"jsdelivr": "./dist/browser.js",
"unpkg": "./dist/browser.js",
"browser": {
"./index.js": "./dist/browser.js"
},
"scripts": {
"build": "tsc",
"clean": "rm -rf dist/"
},
"engines": {
"node": "^14.0.0"
},
"dependencies": {
"@algolia/client-common": "5.0.0",
"@algolia/requester-browser-xhr": "5.0.0",
"@algolia/requester-node-http": "5.0.0"
},
"devDependencies": {
"@types/node": "16.11.11",
"typescript": "4.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Transporter, createAuth, getUserAgent } from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Host,
Request,
} from '@algolia/client-common';

import type { FetchUserProfileResponse } from '../model/fetchUserProfileResponse';
import type { Params } from '../model/params';

export const version = '0.0.1';

function getDefaultHosts(): Host[] {
return [
{
url: 'predict-api-oslcbws3zq-ew.a.run.app',
accept: 'readWrite',
protocol: 'https',
},
];
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const createPredictApi = (options: CreateClientOptions) => {
const auth = createAuth(options.appId, options.apiKey, options.authMode);
const transporter = new Transporter({
hosts: options?.hosts ?? getDefaultHosts(),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
...auth.headers(),
},
baseQueryParameters: auth.queryParameters(),
userAgent: getUserAgent({
userAgents: options.userAgents,
client: 'Predict',
version,
}),
timeouts: options.timeouts,
requester: options.requester,
});

/**
* Get predictions, properties (raw, computed or custom) and segments (computed or custom) for a user profile.
*
* @summary Get user profile.
* @param fetchUserProfile - The fetchUserProfile object.
* @param fetchUserProfile.userID - User ID for authenticated users or cookie ID for non-authenticated repeated users (visitors).
* @param fetchUserProfile.params - The params object.
*/
function fetchUserProfile({
userID,
params,
}: FetchUserProfileProps): Promise<FetchUserProfileResponse> {
const path = '/1/users/{userID}/fetch'.replace(
'{userID}',
encodeURIComponent(String(userID))
);
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (!userID) {
throw new Error(
'Parameter `userID` is required when calling `fetchUserProfile`.'
);
}

if (!params) {
throw new Error(
'Parameter `params` is required when calling `fetchUserProfile`.'
);
}

const request: Request = {
method: 'POST',
path,
data: params,
};

return transporter.request(request, {
queryParameters,
headers,
});
}

return { fetchUserProfile };
};

export type PredictApi = ReturnType<typeof createPredictApi>;

export type FetchUserProfileProps = {
/**
* User ID for authenticated users or cookie ID for non-authenticated repeated users (visitors).
*/
userID: string;
params: Params;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
},
"include": ["src", "model", "node.ts", "browser.ts"],
"exclude": ["dist", "node_modules"]
}
Loading