Skip to content
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

adding samples to the template sdk #10050

Merged
merged 8 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 additions & 0 deletions sdk/template/template/.nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"include": [
"dist-esm/src/**/*.js"
],
"exclude": [
"**/*.d.ts",
"dist-esm/src/generated/*"
],
"exclude-after-remap": false,
"sourceMap": true,
"produce-source-map": true,
"instrument": true,
"all": true
}
17 changes: 0 additions & 17 deletions sdk/template/template/.vscode/launch.json

This file was deleted.

16 changes: 0 additions & 16 deletions sdk/template/template/.vscode/tasks.json

This file was deleted.

59 changes: 43 additions & 16 deletions sdk/template/template/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// https://github.com/karma-runner/karma-chrome-launcher
process.env.CHROME_BIN = require("puppeteer").executablePath();
require("dotenv").config();
const {
jsonRecordingFilterFunction,
isPlaybackMode,
isSoftRecordMode,
isRecordMode
} = require("@azure/test-utils-recorder");
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved

module.exports = function(config) {
config.set({
Expand All @@ -20,17 +27,18 @@ module.exports = function(config) {
"karma-env-preprocessor",
"karma-coverage",
"karma-remap-istanbul",
"karma-junit-reporter"
"karma-junit-reporter",
"karma-json-to-file-reporter",
"karma-json-preprocessor"
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved
],

// list of files / patterns to load in the browser
files: [
// Uncomment the cdn link below for the polyfill service to support IE11 missing features
// Promise,String.prototype.startsWith,String.prototype.endsWith,String.prototype.repeat,String.prototype.includes,Array.prototype.includes,Object.keys
// "https://cdn.polyfill.io/v2/polyfill.js?features=Symbol,Promise,String.prototype.startsWith,String.prototype.endsWith,String.prototype.repeat,String.prototype.includes,Array.prototype.includes,Object.keys|always",
"dist-test/index.browser.js",
{ pattern: "dist-test/index.browser.js.map", type: "html", included: false, served: true }
],
].concat(
isPlaybackMode() || isSoftRecordMode() ? ["recordings/browsers/**/*.json"] : []
),
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved

// list of files / patterns to exclude
exclude: [],
Expand All @@ -39,20 +47,26 @@ module.exports = function(config) {
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"**/*.js": ["env"],
"recordings/browsers/**/*.json": ["json"]
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved
// IMPORTANT: COMMENT following line if you want to debug in your browsers!!
// Preprocess source file to calculate code coverage, however this will make source file unreadable
"dist-test/index.js": ["coverage"]
// "dist-test/index.js": ["coverage"]
},

// inject following environment values into browser testing with window.__env__
// environment values MUST be exported or set with same console running "karma start"
// https://www.npmjs.com/package/karma-env-preprocessor
envPreprocessor: ["ACCOUNT_NAME", "ACCOUNT_SAS"],
envPreprocessor: [
"TEST_MODE",
"ENDPOINT",
"API_KEY",
"API_KEY_ALT",
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_TENANT_ID"
],

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ["mocha", "coverage", "karma-remap-istanbul", "junit"],
reporters: ["mocha", "coverage", "karma-remap-istanbul", "junit", "json-to-file"],
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved

coverageReporter: {
// specify a common output directory
Expand Down Expand Up @@ -80,6 +94,11 @@ module.exports = function(config) {
properties: {} // key value pair of properties to add to the <properties> section of the report
},

jsonToFileReporter: {
filter: jsonRecordingFilterFunction,
outputPath: "."
},
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved

// web server port
port: 9876,

Expand All @@ -93,14 +112,19 @@ module.exports = function(config) {
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
// 'ChromeHeadless', 'Chrome', 'Firefox', 'Edge', 'IE'
browsers: ["ChromeHeadless"],
// --no-sandbox allows our tests to run in Linux without having to change the system.
// --disable-web-security allows us to authenticate from the browser without having to write tests using interactive auth, which would be far more complex.
browsers: ["ChromeHeadlessNoSandbox"],
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: "ChromeHeadless",
flags: ["--no-sandbox", "--disable-web-security"]
}
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
singleRun: false,
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved

// Concurrency level
// how many browser should be started simultaneous
Expand All @@ -109,6 +133,9 @@ module.exports = function(config) {
browserNoActivityTimeout: 600000,
browserDisconnectTimeout: 10000,
browserDisconnectTolerance: 3,
browserConsoleLogOptions: {
terminal: !isRecordMode()
},
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved

client: {
mocha: {
Expand Down
45 changes: 30 additions & 15 deletions sdk/template/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1",
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
"build:samples": "cd samples && tsc -p .",
"build:samples": "node ../../../common/scripts/prep-samples.js && cd samples && tsc -p .",
"build:test": "tsc -p . && rollup -c rollup.test.config.js 2>&1",
"build": "tsc -p . && rollup -c 2>&1 && api-extractor run --local",
"check-format": "prettier --list-different \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-* types *.tgz *.log",
"execute:samples": "echo skipped",
"check-format": "prettier --list-different --config ../../.prettierrc.json --ignore-path ../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-* test-dist temp types *.tgz *.log",
"execute:js-samples": "node ../../../common/scripts/run-samples.js samples/javascript/",
"execute:ts-samples": "node ../../../common/scripts/run-samples.js samples/typescript/dist/samples/typescript/src/",
"execute:samples": "npm run build:samples && npm run execute:js-samples && npm run execute:ts-samples",
"extract-api": "tsc -p . && api-extractor run --local",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "echo skipped",
"integration-test:node": "echo skipped",
"format": "prettier --write --config ../../.prettierrc.json --ignore-path ../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "karma start --single-run",
"integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace dist-esm/test/**/*.spec.js",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"lint:fix": "eslint package.json tsconfig.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
"lint": "eslint package.json tsconfig.json api-extractor.json src test --ext .ts -f html -o template-lintReport.html || exit 0",
Expand All @@ -33,7 +35,7 @@
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
"test": "npm run build:test && npm run unit-test && npm run integration-test",
"unit-test:browser": "karma start --single-run",
"unit-test:node": "mocha --reporter ../../../common/tools/mocha-multi-reporter.js dist-test/index.node.js",
"unit-test:node": "mocha --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace dist-test/index.node.js",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
},
"files": [
Expand All @@ -43,43 +45,54 @@
"README.md",
"LICENSE"
],
"repository": "github:Azure/azure-sdk-for-js",
"repository": {
"type": "git",
"url": "https://github.com/Azure/azure-sdk-for-js.git",
"directory": "sdk/template/template"
},
"keywords": [
"azure",
"cloud",
"Azure"
"Azure",
"typescript"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"engines": {
"node": ">=8.0.0"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/template/template/",
"sideEffects": false,
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
"dependencies": {
"@azure/core-auth": "^1.1.3",
"@azure/core-http": "^1.1.4",
"@azure/core-tracing": "1.0.0-preview.8",
"@azure/logger": "^1.0.0",
"@opentelemetry/api": "^0.6.1",
"events": "^3.0.0",
"tslib": "^2.0.0"
},
"devDependencies": {
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^1.1.0-preview",
"@azure/test-utils-recorder": "^1.0.0",
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved
"@microsoft/api-extractor": "7.7.11",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.2.0",
"@types/chai": "^4.1.6",
"@types/chai-as-promised": "^7.1.0",
"@types/mocha": "^7.0.2",
"@types/node": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"assert": "^1.4.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cross-env": "^7.0.2",
"dotenv": "^8.2.0",
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-no-null": "^1.0.2",
Expand All @@ -99,9 +112,11 @@
"karma-remap-istanbul": "^0.6.0",
"mocha": "^7.1.1",
"mocha-junit-reporter": "^1.18.0",
"nyc": "^14.0.0",
"prettier": "^1.16.4",
"rimraf": "^3.0.0",
"rollup": "^1.16.3",
"rollup-plugin-shim": "^1.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-visualizer": "^4.0.4",
Expand Down
6 changes: 6 additions & 0 deletions sdk/template/template/review/template.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

```ts

import { AzureKeyCredential } from '@azure/core-auth';
import EventEmitter from 'events';
import { KeyCredential } from '@azure/core-auth';
import { URLBuilder } from '@azure/core-http';

export { AzureKeyCredential }

// @public (undocumented)
export function createEventEmitter(): EventEmitter;

export { KeyCredential }

// @public (undocumented)
export function (str: string): void;

Expand Down
17 changes: 17 additions & 0 deletions sdk/template/template/sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Used in most samples. Retrieve these values from a Cognitive Services instance
# in the Azure Portal.
ENDPOINT="Your Endpoint URL"
API_KEY="Your API key"

# Used to authenticate using Azure AD as a service principal for role-based authentication
# in the tokenAuth sample.
#
# See the documentation for `EnvironmentCredential` at the following link:
# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential
AZURE_TENANT_ID=<AD tenant id or name>
AZURE_CLIENT_ID=<ID of the user/service principal to authenticate as>
AZURE_CLIENT_SECRET=<client secret used to authenticate to Azure AD>

# Our tests assume that TEST_MODE is "playback" by default. You can
# change it to "record" to generate new recordings, or "live" to bypass the recorder entirely.
# TEST_MODE=playback
Empty file.
60 changes: 60 additions & 0 deletions sdk/template/template/samples/javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!-- The following YAML bit is needed by the docs system to publish the samples online. Uncomment/Update it when the samples can be published publically -->

<!-- ---
page_type: sample
languages:
- javascript
products:
- azure
- azure-template
urlFragment: template-javascript
--- -->

# Azure Template client library samples for JavaScript

These sample programs show how to use the JavaScript client libraries for Azure Template in some common scenarios.

| **File Name** | **Description** |
| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| [sampleTemplate.js][sampleTemplate] | sample template |

## Prerequisites

The samples are compatible with Node.js >= 8.0.0.

You need [an Azure subscription][freesub] to run these sample programs. Samples retrieve credentials to access the endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].

## Setup

To run the samples using the published version of the package:

1. Install the dependencies using `npm`:

```bash
npm install
```

2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.

3. Run whichever samples you like (note that some samples may require additional setup, see the table above):

```bash
node sampleTemplate.js
```

Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform):

```bash
npx cross-env ENDPOINT="<endpoint>" API_KEY="<api key>" node sampleTemplate.js
```

## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[sampleTemplate]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/template/template/samples/javascript/src/sampleTemplate.js
[apiref]: https://docs.microsoft.com/javascript/api
[freesub]: https://azure.microsoft.com/free/
[package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/template/template/README.md
Loading