Skip to content

Commit

Permalink
extract from apps and externalize
Browse files Browse the repository at this point in the history
Signed-off-by: Victoria French <[email protected]>
  • Loading branch information
deavial committed May 22, 2019
1 parent 0b2706d commit 2bf2994
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]

[lints]

[options]

[strict]
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
67 changes: 67 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# OSX
#
.DS_Store

# BUILDSHIP
#
.project
.classpath
.settings

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace
Pods/
*.log
*Report.txt

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
android/app/release/
android/app/debug/

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle
10 changes: 10 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"useTabs": false,
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"jsxBracketSameLine": false,
"bracketSpacing": true
}

13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Change Log

## HEAD

> [See Changes](https://github.com/placeposition/alpaka-presence/compare/v1.0.0...master)
* No Changes

## v1.0.0 (May 22, 2019)

> [See Changes](https://github.com/placeposition/alpaka-presence/compare/0b2706d...v1.0.0)
* **ADDED** Initial codebase extracted from existing projects.
101 changes: 100 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,101 @@
# alpaka-native-useragent
User Agent builder used by Alpaka React Native apps.

[![npm version](https://badge.fury.io/js/@alpakaio/alpaka-native-useragent.svg)](http://badge.fury.io/js/@alpakaio/alpaka-native-useragent)

User Agent builder used by [Alpaka](https://www.alpaka.io) [React Native](https://github.com/facebook/react-native) apps.

## TOC

* [Installation](#installation)
* [Usage](#usage)
* [API](#api)
* [Troubleshooting](#troubleshooting)
* [Release Notes](#release-notes)
* [react-native-dom / react-native-web](#react-native-dom)

## Installation

Using npm:

```shell
npm install --save @alpakaio/alpaka-native-useragent
```

> ⚠️ This package has a peer dependency of [React Native Device Info]() at version 2.0.0 or higher.
## Usage

```js
import UserAgent from '@alpakaio/alpaka-native-useragent';
```

## API

| Method | Return Type |  iOS | Android | Windows | Since |
| ----------------------------------------------------------------- | ------------------- | :--: | :-----: | :-----: | ------ |
| [getUserAgentAsync()](#getuseragentasync) | `Promise<string>` |||| 1.0.0 |

### getUserAgentAsync()

Gets the appropriate user-agent string to use with Alpaka services.

**Examples**

```js
const userAgentString = await UserAgent.getUserAgentAsync();
// "Alpaka Punch; 1.0.0; b.17; en-GB; (Apple; iOS; 12.1; Handheld; iPhone(12,1))"
```

```js
UserAgent.getUserAgentAsync().then((userAgentString) => {
// "Alpaka Punch; 1.0.0; b.17; en-GB; (Apple; iOS; 12.1; Handheld; iPhone(12,1))"
});
```

## Troubleshooting

When installing or using `alpaka-native-useragent`, you may encounter the following problems:

<details>
<summary>[tests] - Cannot run my test suite when using this library</summary>

`alpaka-native-useragent` relies on `react-native-device-info` which contains native code, and needs to be mocked. Jest Snapshot support may work though.

Here's how to do it with jest for example:

```js
// in your package.json:
"jest": {
"setupFiles": [
"./testenv.js"
],


// testenv.js:
jest.mock('@alpakaio/alpaka-native-useragent', () => {
return {
getUserAgentAsync: jest.fn(),
};
});
```
</details>
<details>
<summary>[warnings] - dispatch_sync</summary>
Some of the APIs internally used will throw warnings in certain conditions like on tvOS or the iOS emulator. This won't be visible in production but even in development it may be irritating. It is useful to have the warnings because these devices return no state, and that can be surprising, leading to github support issues. The warnings is intended to educate you as a developer. If the warnings are troublesome you may try this in your code to suppress them:
```javascript
import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings(['Required dispatch_sync to load constants for RNDeviceInfo']);
```
</details>
## Release Notes
See the [CHANGELOG.md](https://github.com/alpakaio/alpaka-native-useragent/blob/master/CHANGELOG.md).
## react-native-dom
This library was made compatible with [react-native-dom](https://github.com/vincentriemer/react-native-dom) and [react-native-web](https://github.com/necolas/react-native-web) by providing an empty polyfill in order to avoid breaking builds.
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@alpakaio/alpaka-native-useragent",
"version": "1.0.0",
"description": "User Agent builder used by Alpaka React Native apps",
"main": "useragent.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alpakaio/alpaka-native-useragent.git"
},
"author": {
"name": "Victoria French",
"email": "[email protected]",
"url": "https://github.com/victoriafrench"
},
"contributors": [
{
"name": "Victoria French",
"email": "[email protected]",
"url": "https://github.com/victoriafrench"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/alpakaio/alpaka-native-useragent/issues"
},
"homepage": "https://github.com/alpakaio/alpaka-native-useragent#readme",
"keywords": [
"react-native",
"ios",
"android",
"windows",
"tvos",
"device",
"alpaka"
],
"peerDependencies": {
"react-native-device-info": "^2.0.0"
},
"typings": "./useragent.d.ts",
"scripts": {
"flow-check": "npx flow check-contents < useragent.js.flow",
"ts-check": "npx tsc useragent.d.ts --noEmit"
}
}
8 changes: 8 additions & 0 deletions useragent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// should be imported this way:
// import UserAgent from '@alpakaio/alpaka-native-useragent';

declare const _default: {
getUserAgentAsync: () => Promise<string>;
};

export default _default;
19 changes: 19 additions & 0 deletions useragent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Platform } from 'react-native';
import DeviceInfo from 'react-native-device-info';

const state = {
userAgent: null,
};

export default {
getUserAgentAsync: async () => {
if (!state.userAgent) {
if (Platform.OS === 'web' || Platform.OS === 'dom') {
state.userAgent = DeviceInfo.getUserAgent();
} else {
state.userAgent = `${DeviceInfo.getApplicationName()}; ${DeviceInfo.getVersion()}; b.${DeviceInfo.getBuildNumber()}; ${DeviceInfo.getDeviceLocale()}; (${DeviceInfo.getBrand()}; ${DeviceInfo.getSystemName()}; ${DeviceInfo.getSystemVersion()}; ${DeviceInfo.getDeviceType()}; ${DeviceInfo.getDeviceId()})`;
}
}
return state.userAgent;
}
};
5 changes: 5 additions & 0 deletions useragent.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow

declare module.exports: {
getUserAgentAsync: () => Promise<string>,
};

0 comments on commit 2bf2994

Please sign in to comment.