Skip to content

Commit

Permalink
feat(javascript): remove classes usage (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Feb 22, 2022
1 parent acdd483 commit 48571bb
Show file tree
Hide file tree
Showing 49 changed files with 479 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.00KB"
},
{
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
Expand All @@ -30,11 +30,11 @@
},
{
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.25KB"
"maxSize": "3.00KB"
},
{
"path": "packages/client-common/dist/client-common.esm.node.js",
"maxSize": "3.25KB"
"maxSize": "3.00KB"
},
{
"path": "packages/requester-browser-xhr/dist/requester-browser-xhr.esm.node.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Host, Requester } from '@algolia/client-common';
import { XhrRequester } from '@algolia/requester-browser-xhr';
import { createXhrRequester } from '@algolia/requester-browser-xhr';

import { createAbtestingApi } from '../src/abtestingApi';
import type { AbtestingApi, Region } from '../src/abtestingApi';
Expand Down Expand Up @@ -29,7 +29,7 @@ export function abtestingApi(
read: 2,
write: 30,
},
requester: options?.requester ?? new XhrRequester(),
requester: options?.requester ?? createXhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
...options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Host, Requester } from '@algolia/client-common';
import { HttpRequester } from '@algolia/requester-node-http';
import { createHttpRequester } from '@algolia/requester-node-http';

import { createAbtestingApi } from '../src/abtestingApi';
import type { AbtestingApi, Region } from '../src/abtestingApi';
Expand Down Expand Up @@ -29,7 +29,7 @@ export function abtestingApi(
read: 5,
write: 30,
},
requester: options?.requester ?? new HttpRequester(),
requester: options?.requester ?? createHttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
...options,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Transporter, createAuth, getUserAgent } from '@algolia/client-common';
import {
createAuth,
createMemoryCache,
createTransporter,
getUserAgent,
} from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Expand Down Expand Up @@ -32,8 +37,9 @@ export function createAbtestingApi(
options: CreateClientOptions & { region?: Region }
) {
const auth = createAuth(options.appId, options.apiKey, options.authMode);
const transporter = new Transporter({
const transporter = createTransporter({
hosts: options?.hosts ?? getDefaultHosts(options.region),
hostsCache: createMemoryCache(),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
...auth.headers(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Host, Requester } from '@algolia/client-common';
import { XhrRequester } from '@algolia/requester-browser-xhr';
import { createXhrRequester } from '@algolia/requester-browser-xhr';

import { createAnalyticsApi } from '../src/analyticsApi';
import type { AnalyticsApi, Region } from '../src/analyticsApi';
Expand Down Expand Up @@ -29,7 +29,7 @@ export function analyticsApi(
read: 2,
write: 30,
},
requester: options?.requester ?? new XhrRequester(),
requester: options?.requester ?? createXhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
...options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Host, Requester } from '@algolia/client-common';
import { HttpRequester } from '@algolia/requester-node-http';
import { createHttpRequester } from '@algolia/requester-node-http';

import { createAnalyticsApi } from '../src/analyticsApi';
import type { AnalyticsApi, Region } from '../src/analyticsApi';
Expand Down Expand Up @@ -29,7 +29,7 @@ export function analyticsApi(
read: 5,
write: 30,
},
requester: options?.requester ?? new HttpRequester(),
requester: options?.requester ?? createHttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
...options,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Transporter, createAuth, getUserAgent } from '@algolia/client-common';
import {
createAuth,
createMemoryCache,
createTransporter,
getUserAgent,
} from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Expand Down Expand Up @@ -47,8 +52,9 @@ export function createAnalyticsApi(
options: CreateClientOptions & { region?: Region }
) {
const auth = createAuth(options.appId, options.apiKey, options.authMode);
const transporter = new Transporter({
const transporter = createTransporter({
hosts: options?.hosts ?? getDefaultHosts(options.region),
hostsCache: createMemoryCache(),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
...auth.headers(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export * from './src/createMemoryCache';
export * from './src/createAuth';
export * from './src/createEchoRequester';
export * from './src/createMemoryCache';
export * from './src/createStatefulHost';
export * from './src/createTransporter';
export * from './src/createUserAgent';
export * from './src/errors';
export * from './src/getUserAgent';
export * from './src/helpers';
export * from './src/Requester';
export * from './src/Response';
export * from './src/stackTrace';
export * from './src/StatefulHost';
export * from './src/Transporter';
export * from './src/types';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
export type Cache = {
/**
* Gets the value of the given `key`.
*/
get: <TValue>(
key: Record<string, any> | string,
defaultValue: () => Promise<TValue>
) => Promise<TValue>;

/**
* Sets the given value with the given `key`.
*/
set: <TValue>(
key: Record<string, any> | string,
value: TValue
) => Promise<TValue>;

/**
* Deletes the given `key`.
*/
delete: (key: Record<string, any> | string) => Promise<void>;

/**
* Clears the cache.
*/
clear: () => Promise<void>;
};
import type { Cache } from './types';

export function createMemoryCache(): Cache {
let cache: Record<string, any> = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Host, StatefulHost } from './types';

// By default, API Clients at Algolia have expiration delay of 5 mins.
// In the JavaScript client, we have 2 mins.
const EXPIRATION_DELAY = 2 * 60 * 1000;

export function createStatefulHost(
host: Host,
status: StatefulHost['status'] = 'up'
): StatefulHost {
const lastUpdate = Date.now();

function isUp(): boolean {
return status === 'up' || Date.now() - lastUpdate > EXPIRATION_DELAY;
}

function isTimedout(): boolean {
return status === 'timedout' && Date.now() - lastUpdate <= EXPIRATION_DELAY;
}

return { ...host, status, lastUpdate, isUp, isTimedout };
}
Loading

0 comments on commit 48571bb

Please sign in to comment.