Skip to content

Commit

Permalink
Use cross platform service (#14)
Browse files Browse the repository at this point in the history
* fix typescript errors

* add basic client data context

* update rollup configuration

* test basic data context

* test data context

* bump @universis/query

* change author

* fix build process

* update .npmignore

* update tests

* fix lint errors

* fix karma test

* add coverage

* fix order by descending statement

* npm ignore

* 2.12.0
  • Loading branch information
kbarbounakis authored Dec 8, 2023
1 parent 43626d8 commit 06f688d
Show file tree
Hide file tree
Showing 29 changed files with 7,796 additions and 818 deletions.
9 changes: 9 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.eslintrc.js
karma.conf.js
spec/helpers/*.js

# rollup
rollup.config.js

# dist
dist
163 changes: 163 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
module.exports = {
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"eslint-plugin-jsdoc",
"eslint-plugin-prefer-arrow",
"@typescript-eslint"
],
"root": true,
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [
"error",
{
"default": "array"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"types": {
"Object": {
"message": "Avoid using the `Object` type. Did you mean `object`?"
},
"Function": {
"message": "Avoid using the `Function` type. Prefer a specific function type, like `() => void`."
},
"Boolean": {
"message": "Avoid using the `Boolean` type. Did you mean `boolean`?"
},
"Number": {
"message": "Avoid using the `Number` type. Did you mean `number`?"
},
"String": {
"message": "Avoid using the `String` type. Did you mean `string`?"
},
"Symbol": {
"message": "Avoid using the `Symbol` type. Did you mean `symbol`?"
}
}
}
],
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/dot-notation": "error",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/naming-convention": [
"off",
{
"selector": "variable",
"format": [
"camelCase",
"UPPER_CASE"
],
"leadingUnderscore": "forbid",
"trailingUnderscore": "forbid"
}
],
"@typescript-eslint/no-unsafe-argument": "off", // todo: enable
"@typescript-eslint/no-unsafe-return": "off", // todo: enable
"@typescript-eslint/no-unsafe-assignment": "off", // todo: enable
"@typescript-eslint/no-empty-function": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-shadow": [
"error",
{
"hoist": "all"
}
],
"@typescript-eslint/no-unused-expressions": "error",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/triple-slash-reference": [
"error",
{
"path": "always",
"types": "prefer-import",
"lib": "always"
}
],
"@typescript-eslint/typedef": "off",
"@typescript-eslint/unified-signatures": "error",
"complexity": "off",
"constructor-super": "error",
"dot-notation": "off",
"eqeqeq": [
"error",
"smart"
],
"guard-for-in": "error",
"id-denylist": "off",
"id-match": "off",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
"jsdoc/newline-after-description": "off",
"max-classes-per-file": "off",
"new-parens": "error",
"no-bitwise": "off",
"no-caller": "error",
"no-cond-assign": "error",
"no-console": "error",
"no-debugger": "error",
"no-empty": "error",
"no-empty-function": "off",
"no-eval": "error",
"no-fallthrough": "off",
"no-invalid-this": "off",
"no-new-wrappers": "error",
"no-shadow": "off",
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-underscore-dangle": "off",
"no-unsafe-finally": "error",
"no-unused-expressions": "off",
"no-unused-labels": "error",
"no-use-before-define": "off",
"no-var": "error",
"object-shorthand": "error",
"one-var": [
"error",
"never"
],
"prefer-arrow/prefer-arrow-functions": [
"error",
{
"allowStandaloneDeclarations": true
}
],
"prefer-const": "error",
"radix": "error",
"spaced-comment": [
"error",
"always",
{
"markers": [
"/"
]
}
],
"use-isnan": "error",
"valid-typeof": "off"
}
};
11 changes: 10 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,28 @@ docs
src/test
spec
src/test.ts
.env.test
.env

# karma
karma.conf.js
karma-test-api-server.js

# build
rollup.config.js

# typescript
tsconfig.json
tsconfig.lib.json
tsconfig.spec.json

# tslint
tslint.json
.eslintignore
.eslintrc.js

# .gitpod
.gitpod.yml
.gitpod.yml

# tmp
.pytest_cache
9 changes: 9 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"module": "dist/index.esm.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "rimraf dist && rollup -c ./rollup.config.js -m"
}
}
11 changes: 11 additions & 0 deletions common/src/BasicDataContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ClientDataContext } from '@themost/client';
import { ClientDataContextOptions } from '@themost/client';
import { BasicDataService } from './BasicDataService';

class BasicDataContext extends ClientDataContext {
constructor(base: string, options?: ClientDataContextOptions) {
super(new BasicDataService(base, options), options);
}
}

export { BasicDataContext };
115 changes: 115 additions & 0 deletions common/src/BasicDataService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { ClientDataService } from '@themost/client';
import { ClientDataContextOptions, DataServiceExecuteOptions, DataServiceHeaders, ResponseError } from '@themost/client';
import { EdmSchema } from '@themost/client';
import axios, { AxiosRequestConfig } from 'axios';
import { Buffer } from 'buffer';

/**
* Represents a default data service that extends the ClientDataService class.
*/
class BasicDataService extends ClientDataService {

// eslint-disable-next-line no-useless-constructor
constructor(base: string, options?: ClientDataContextOptions) {
super(base, options);
}

/**
* Executes a request to the current data service
* @param {DataServiceExecuteOptions} options - An object that represents the request options
* @returns {Promise<any>}
*/
async execute(options: DataServiceExecuteOptions): Promise<any> {
// get absolute url
const config: { method: string; url: string; headers: DataServiceHeaders; } = {
method: options.method,
url: this.resolve(options.url),
headers: Object.assign({}, this.getHeaders()) // set service headers
};
// assign options headers if any
if (options.headers) {
Object.assign(config.headers, options.headers);
}

if (options.method === 'POST' || options.method === 'PUT' || options.method === 'DELETE') {
// add body
if (options.data != null) {
Object.assign(config, {
data: options.data
});
}
} else {
// for HEAD, GET, OPTIONS, DELETE set query params
if (options.data != null) {
const queryParams = new URLSearchParams();
const data: { [key: string]: any; } = options.data;
Object.keys(data)
.filter((key) => {
return Object.prototype.hasOwnProperty.call(data, key);
})
.forEach((key) => {
queryParams.append(key, data[key]);
});
// assign url with params
Object.assign(config, {
url: config.url + '?' + queryParams.toString()
});
}
}
const reviver = this.getOptions().useJsonReviver;
if (typeof reviver === 'function') {
Object.assign(config, {
responseType: 'arraybuffer'
});
}
const response = await axios(config as AxiosRequestConfig);
if (response.status === 204) {
return null;
} else if (response.status === 200) {
if (typeof reviver === 'function') {
// get response content type
const contentType: string | undefined = response.headers['content-type'];
if (contentType != null &&
(contentType.match(/^application\/json;?/) ||
contentType.match(/^application\/ld\+json;?/))) {
const buffer = Buffer.from(response.data, 'binary');
return JSON.parse(buffer.toString(), reviver);
}
}
// otherwise return response data
return response.data;
}
// otherwise throw error
throw new ResponseError(
'An error occurred while getting service metadata',
response.status
);
}

/**
* Gets the metadata of the current data service
* @returns {Promise<EdmSchema>}
*/
async getMetadata(): Promise<EdmSchema> {
const config: any = {
method: 'GET',
url: this.resolve('$metadata'),
headers: this.getHeaders()
};
// get response
const response = await axios(config);
if (response.status === 200) {
// load schema
return EdmSchema.loadXML(response.data) as EdmSchema;
}
// otherwise throw error
throw new ResponseError(
'An error occurred while getting service metadata',
response.status
);
}
}

export {
BasicDataService
}
2 changes: 2 additions & 0 deletions common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './BasicDataService';
export * from './BasicDataContext';
30 changes: 30 additions & 0 deletions common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist/",
"target": "ES2015",
"module": "ESNext",
"sourceMap": true,
"esModuleInterop": true,
"declaration": false,
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": [
"DOM",
"ES2017"
],
"paths": {
"@themost/client": [
"../src/index"
]
}
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts"
],
"exclude": [
"node_modules"
]
}
10 changes: 10 additions & 0 deletions common/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"paths": {
"@themost/client": [
".."
]
}
}
}
Loading

0 comments on commit 06f688d

Please sign in to comment.