Skip to content

feat: Add Insights client specs + javascript insights client #62

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 14 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
- name: Checking analytics specs
run: yarn build:specs analytics

- name: Checking insights specs
run: yarn build:specs insights

- name: Lint
run: yarn specs:lint

Expand Down Expand Up @@ -72,6 +75,9 @@ jobs:
- name: Build analytics client
run: yarn build:clients javascript analytics

- name: Build insights client
run: yarn build:clients javascript insights

- name: Lint
run: yarn lint

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
.openapi-generator
.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is the entrypoint for the package
export * from './src/apis';
export * from './model/models';
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 openapi-petstore-perl "minor update" "gitlab.com"

git_user_id=$1
git_repo_id=$2
release_note=$3
git_host=$4

if [ "$git_host" = "" ]; then
git_host="algolia"
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
fi

if [ "$git_user_id" = "" ]; then
git_user_id="algolia"
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
fi

if [ "$git_repo_id" = "" ]; then
git_repo_id="algoliasearch-client-javascript"
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
fi

if [ "$release_note" = "" ]; then
release_note="Minor update"
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
fi

# Initialize the local directory as a Git repository
git init

# Adds the files in the local repository and stages them for commit.
git add .

# Commits the tracked changes and prepares them to be pushed to a remote repository.
git commit -m "$release_note"

# Sets the new remote
git_remote=$(git remote)
if [ "$git_remote" = "" ]; then # git remote not defined

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:"${GIT_TOKEN}"@${git_host}/${git_user_id}/${git_repo_id}.git
fi

fi

git pull origin master

# Pushes (Forces) the changes in the local repository up to the remote repository
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
git push origin master 2>&1 | grep -v 'To https'
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,49 @@
/**
* Insights event.
*/
export type InsightEvent = {
/**
* An eventType can be a click, a conversion, or a view.
*/
eventType: InsightEvent.EventTypeEnum;
/**
* A user-defined string used to categorize events.
*/
eventName: string;
/**
* Name of the targeted index.
*/
index: string;
/**
* A user identifier. Depending if the user is logged-in or not, several strategies can be used from a sessionId to a technical identifier.
*/
userToken: string;
/**
* Time of the event expressed in milliseconds since the Unix epoch.
*/
timestamp?: number;
/**
* Algolia queryID. This is required when an event is tied to a search.
*/
queryID?: string;
/**
* An array of index objectID. Limited to 20 objects. An event can’t have both objectIDs and filters at the same time.
*/
objectIDs?: string[];
/**
* An array of filters. Limited to 10 filters. An event can’t have both objectIDs and filters at the same time.
*/
filters?: string[];
/**
* Position of the click in the list of Algolia search results. This field is required if a queryID is provided. One position must be provided for each objectID.
*/
positions?: number[];
};

export namespace InsightEvent {
export enum EventTypeEnum {
Click = 'click',
Conversion = 'conversion',
View = 'view',
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { InsightEvent } from './insightEvent';

/**
* Object containing the events sent to the Insights API.
*/
export type InsightEvents = {
/**
* Array of events sent to the Insights API.
*/
events: InsightEvent[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable no-param-reassign */
import type { RequestOptions } from '../utils/types';

export * from './errorBase';
export * from './insightEvent';
export * from './insightEvents';
export * from './pushEventsResponse';

export interface Authentication {
/**
* Apply authentication settings to header and query params.
*/
applyToRequest: (requestOptions: RequestOptions) => Promise<void> | void;
}

export class ApiKeyAuth implements Authentication {
apiKey: string = '';

constructor(private location: string, private paramName: string) {}

applyToRequest(requestOptions: RequestOptions): void {
if (this.location === 'query') {
requestOptions.queryParameters[this.paramName] = this.apiKey;
} else if (
this.location === 'header' &&
requestOptions &&
requestOptions.headers
) {
requestOptions.headers[this.paramName] = this.apiKey;
} else if (
this.location === 'cookie' &&
requestOptions &&
requestOptions.headers
) {
if (requestOptions.headers.Cookie) {
requestOptions.headers.Cookie += `; ${
this.paramName
}=${encodeURIComponent(this.apiKey)}`;
} else {
requestOptions.headers.Cookie = `${this.paramName}=${encodeURIComponent(
this.apiKey
)}`;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type PushEventsResponse = {
/**
* A message confirming the event push.
*/
message: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@algolia/client-insights",
"version": "5.0.0",
"description": "JavaScript client for @algolia/client-insights",
"repository": "algolia/algoliasearch-client-javascript",
"author": "Algolia",
"private": true,
"license": "MIT",
"main": "dist/api.js",
"types": "dist/api.d.ts",
"scripts": {
"clean": "rm -Rf node_modules/ *.js",
"build": "tsc",
"test": "yarn build && node dist/client.js"
},
"engines": {
"node": "^16.0.0",
"yarn": "^3.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,7 @@
import { InsightsApi } from './insightsApi';

export * from './insightsApi';
export * from '../utils/errors';
export { EchoRequester } from '../utils/requester/EchoRequester';

export const APIS = [InsightsApi];
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import type { InsightEvents } from '../model/insightEvents';
import { ApiKeyAuth } from '../model/models';
import type { PushEventsResponse } from '../model/pushEventsResponse';
import { Transporter } from '../utils/Transporter';
import type { Requester } from '../utils/requester/Requester';
import type { Headers, Host, Request, RequestOptions } from '../utils/types';

export enum InsightsApiKeys {
apiKey,
appId,
}

export class InsightsApi {
protected authentications = {
apiKey: new ApiKeyAuth('header', 'X-Algolia-API-Key'),
appId: new ApiKeyAuth('header', 'X-Algolia-Application-Id'),
};

private transporter: Transporter;

private sendRequest<TResponse>(
request: Request,
requestOptions: RequestOptions
): Promise<TResponse> {
if (this.authentications.apiKey.apiKey) {
this.authentications.apiKey.applyToRequest(requestOptions);
}

if (this.authentications.appId.apiKey) {
this.authentications.appId.applyToRequest(requestOptions);
}

return this.transporter.request(request, requestOptions);
}

constructor(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
) {
this.setApiKey(InsightsApiKeys.appId, appId);
this.setApiKey(InsightsApiKeys.apiKey, apiKey);

this.transporter = new Transporter({
hosts: options?.hosts ?? this.getDefaultHosts(),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
},
userAgent: 'Algolia for Javascript',
timeouts: {
connect: 2,
read: 5,
write: 30,
},
requester: options?.requester,
});
}

getDefaultHosts(): Host[] {
return [
{ url: `insights.algolia.io`, accept: 'readWrite', protocol: 'https' },
];
}

setRequest(requester: Requester): void {
this.transporter.setRequester(requester);
}

setHosts(hosts: Host[]): void {
this.transporter.setHosts(hosts);
}

setApiKey(key: InsightsApiKeys, value: string): void {
this.authentications[InsightsApiKeys[key]].apiKey = value;
}

/**
* This command pushes an array of events to the Insights API.
*
* @summary This command pushes an array of events to the Insights API.
* @param pushEvents - The pushEvents parameters.
* @param pushEvents.insightEvents - The insightEvents.
*/
pushEvents({ insightEvents }: PushEventsProps): Promise<PushEventsResponse> {
const path = '/1/events';
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (insightEvents === null || insightEvents === undefined) {
throw new Error(
'Required parameter insightEvents was null or undefined when calling pushEvents.'
);
}

if (insightEvents.events === null || insightEvents.events === undefined) {
throw new Error(
'Required parameter insightEvents.events was null or undefined when calling pushEvents.'
);
}

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

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

return this.sendRequest(request, requestOptions);
}
}

export type PushEventsProps = {
insightEvents: InsightEvents;
};
Loading