Skip to content

Commit

Permalink
Merge pull request #358 from viktor44/main
Browse files Browse the repository at this point in the history
Moving SDK to the new place
  • Loading branch information
viktor44 authored Apr 2, 2024
2 parents e9f0e3f + 9fd31d8 commit 810e1ed
Show file tree
Hide file tree
Showing 32 changed files with 1,258 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/sdk/.codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "2"
prepare:
fetch:
- url: 'https://raw.githubusercontent.com/getstation/tslint-config-station/master/tslint.json'
path: 'tslint.json'
checks:
return-statements:
enabled: false
plugins:
tslint:
enabled: true
config: tslint.json
65 changes: 65 additions & 0 deletions packages/sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# IDE
.idea/

# Build
lib/

11 changes: 11 additions & 0 deletions packages/sdk/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules/
npm-debug.log
yarn-error.log
.vscode/
tests/
src/
.git*
.jshint*
.npmignore
.travis.yml
*.tar.gz
64 changes: 64 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Change Log
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [UNRELEASED]
### Added
- Added `tabs.create` method
- Added ServicesConsumer

## [0.8.0] - 2018-12-12
### Added
- Added silent option for `tabs.navToTab` method
- Added `sdk.tabs.updateTab`
### Removed
- Removed `sdk.tabs.dispatchURLInTab` which was misleading and not used

## [0.6.1] - 2018-08-27
### Added
- Tabs API can now be observed
- Tabs API nav Observable

## [0.6.0] - 2018-08-27
### Added
- Session API

## [0.5.0] - 2018-08-24
### Added
- Activity API

## [0.4.1] - 2018-08-16
### Added
- Add context field to search result items

## [0.4.0] - 2018-06-07
### Added
- Multiple GDrive accounts support

## [0.3.0] - 2018-06-04
### Added
- IPC Consumer
- Add context field in SearchResultItem

## [0.2.1] - 2018-05-31
### Fixed
- add `id` to search consumer

## [0.2.0] - 2018-05-29
### Changed
- `storage` now follows [WebExtensions API](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local)

## [0.1.0] - 2018-05-24
### Changed
- SDK is now instanciated by browserX only

## [0.0.9] - 2018-05-24
### Added
- Tabs Consumer

## [0.0.1] to [0.0.8] - 2018
### Added
- Storage Consumer
- Search Consumer
4 changes: 4 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
⚠️ Work in progress

# sdk
Station SDK
39 changes: 39 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@getstation/sdk",
"version": "0.16.0",
"author": "[email protected]",
"license": "ISC",
"description": "Station SDK",
"main": "./lib/index",
"types": "./lib/index",
"bugs": {
"url": "https://github.com/getstation/sdk/issues"
},
"homepage": "https://github.com/getstation/sdk#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/getstation/sdk.git"
},
"scripts": {
"build": "tsc -p .",
"prepublishOnly": "yarn run lint && yarn run build",
"lint": "tslint -p ."
},
"peerDependencies": {
"rxjs": "^6.0.0"
},
"dependencies": {
"tslib": "^1.9.3"
},
"devDependencies": {
"@types/react": "^16.3.14",
"@types/react-dom": "^16.0.5",
"rxjs": "^6.4.0",
"tslint": "^5.12.1",
"tslint-config-station": "^0.6.0",
"typescript": "^3.9.4"
},
"toolchain": {
"node": "10.15.3"
}
}
54 changes: 54 additions & 0 deletions packages/sdk/src/activity/consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { from, Observable } from 'rxjs';
import { flatMap } from 'rxjs/operators';

import { Consumer, DefaultWeakMap } from '../common';

import { activity } from './index';

import QueryArgs = activity.QueryArgs;
import ActivityEntry = activity.ActivityEntry;

const protectedProvidersWeakMap = new DefaultWeakMap<activity.ActivityConsumer, activity.ActivityProviderInterface>();

export class ActivityConsumer extends Consumer implements activity.ActivityConsumer {
public readonly namespace = 'activity';

push(resourceId: string, extraData?: object, type?: string, manifestURL?: string): Promise<{ activityEntryId: string }> {
return protectedProvidersWeakMap.get(this)!.push({
type: type || '',
createdAt: Date.now(),
resourceId,
manifestURL: manifestURL,
extraData,
});
}

query(userQueryArgs: Partial<QueryArgs> = {}): Observable<ActivityEntry[]> {
const queryArgs: QueryArgs = {
orderBy: 'createdAt',
ascending: false,
limit: 1,
global: false,
where: {
resourceIds: null,
manifestURLs: null,
types: null,
},
whereNot: {
resourceIds: null,
manifestURLs: null,
types: null,
},
...userQueryArgs,
};

const consumer = protectedProvidersWeakMap.get(this)!;

return from(consumer.query(queryArgs))
.pipe(flatMap(c => c));
}

setProviderInterface(providerInterface: activity.ActivityProviderInterface) {
protectedProvidersWeakMap.set(this, providerInterface);
}
}
65 changes: 65 additions & 0 deletions packages/sdk/src/activity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Observable } from 'rxjs';

import { Consumer } from '../common';

export namespace activity {
export interface ActivityConsumer extends Consumer {
/**
* Will push an activity entry for the given search result item.
*
* @param resourceId the id of the SearchResultItem this activity corresponds to
* @param manifestURL the manifestURL corresponding to the resource id
* @param extraData a set of key-value that can be used for anything
* @param type of activity
* @returns a promise with the activityEntryId (which represents the id in sqlite db)
*/
push(resourceId: string, extraData?: any, type?: string, manifestURL?: string): Promise<{ activityEntryId: string }>;

/**
* Query the last **n** activity logs of the plugin.
*
* @example
* // get the last 10 pushed activity entries
* sdk.activity.query({limit: 10, ascending: false}).subscribe(items => {
* doSomething(items);
* })
*
* @param query a QueryArgs item
* @returns an observable of array of ActivityEntry
*/
query(query: QueryArgs): Observable<ActivityEntry[]>;

setProviderInterface(providerInterface: ActivityProviderInterface): void;
}

export interface ActivityEntry {
resourceId: string, // the id of the SearchResultItem this activity corresponds to
manifestURL?: string, // the service id corresponding to the resource id
type: string, // type of activity entry
extraData?: any, // a set of key-value that can be used for anything
createdAt: number, // timestamp at which the activity was made. By default, at the time the function is called
}

export type ScopeFilter = string[] | string | null;

export type QueryArgsScope = {
resourceIds: ScopeFilter,
manifestURLs: ScopeFilter,
types: ScopeFilter,
};

export interface QueryArgs {
orderBy: 'createdAt', // get the last n results or the first n results
ascending: boolean, // ascending or descending (default to false)
limit: number, // limit to the n first result (default to 1)
limitByDate?: number // timestamp in ms, limit only newer activity form thid date
global: boolean, // global or plugin activity (default to false)
where: Partial<QueryArgsScope>,
whereNot: Partial<QueryArgsScope>,
}

export interface ActivityProviderInterface {
push(activityEntry: ActivityEntry): Promise<{ activityEntryId: string }>;
query(query: QueryArgs): Promise<Observable<ActivityEntry[]>>;
}
}
27 changes: 27 additions & 0 deletions packages/sdk/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class Consumer {
public readonly namespace: string;
public readonly id: string;

constructor(id: string) {
this.id = id;
}
}

const defaultWeakMapValue: any = new Proxy({}, {
get(_obj: any, prop: string | symbol) {
console.warn(`${String(prop)} provider not implemented`);
return defaultWeakMapValue;
},
apply() {
return () => defaultWeakMapValue;
},
});

export class DefaultWeakMap<K extends object, V> extends WeakMap<K, V> {
get(key: K): V | undefined {
if (super.has(key)) {
return super.get(key);
}
return defaultWeakMapValue;
}
}
21 changes: 21 additions & 0 deletions packages/sdk/src/config/consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Consumer, DefaultWeakMap } from '../common';

import { config } from './index';

const protectedProvidersWeakMap = new DefaultWeakMap<ConfigConsumer, config.ConfigProviderInterface>();

export class ConfigConsumer extends Consumer implements config.ConfigConsumer {
public readonly namespace = 'config';

get configData() {
return protectedProvidersWeakMap.get(this)!.configData;
}

setProviderInterface(providerInterface: config.ConfigProviderInterface) {
protectedProvidersWeakMap.set(this, providerInterface);
}

setIcon(applicationId: string, url: string): void {
return protectedProvidersWeakMap.get(this)!.setIcon(applicationId, url);
}
}
Loading

0 comments on commit 810e1ed

Please sign in to comment.